473,412 Members | 5,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

WebControl Click Event Not Firing

I have created a web control that can be rendered as either a linkbutton or a
button. It is a ConfirmButton control that allows a developer to force a user
to confirm if they intended to click it such as when they do a delete.

Everything is great. By and large it will be used in my repeater controls
using the command event when the user clicks on it and so that event is
working great.

My issue is the Click event. When the control is either in a repeater or by
itself on a page the click event does not fire. It never even gets raised by
the ConfirmButton control, never mind the listener (ie. page container).

Does anyone have a clue as to why the RaisePostBackEvent never fires? That
is my ultimate question here.

HTML output:

<input type="submit" name="Confirmbutton1" value="Go" id="Confirmbutton1"
onclick="return __doConfirm();__doPostBack('MyPage:Confirmbutton1' ,'')" />

Page using the control event wiring:

private void InitializeComponent()
{
this.Confirmbutton1.Click += new
System.EventHandler(this.Confirmbutton1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
private void Confirmbutton1_Click(object sender, System.EventArgs e)
{
Response.Write("Click event fired!");
}

Server Control Code:

using System;
using System.Text;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace WS.Ops.Web.CustomControls
{
/// <summary>
/// Button that provides a client-side confirmation prompt. It can be
either a Button or LinkButton.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:ConfirmButton1 runat=server></{0}:ConfirmButton1>"),
Description("Button that provides a client-side confirmation prompt. It can
be either a Button or LinkButton.")]
public class ConfirmButton : System.Web.UI.WebControls.WebControl,
IPostBackEventHandler
{
//the confirm button render types
public enum RenderTypes
{
LinkButton,
Button
};

#region Fields
private RenderTypes renderType = RenderTypes.LinkButton;
private WebControl controlToRender;
private const string CONFIRMSCRIPTKEY = "ConfirmScript";
private const string CONFIRMFUNCTIONNAME = "__doConfirm";
private string confirmationText = "Are you sure you want to continue?";
private string text = string.Empty;
private string navigateUrl = string.Empty;
private string commandArgument = string.Empty;
private string commandName = string.Empty;
private bool enableConfirmation = true;
private bool causesValidation = true;
private static readonly object EventCommand;
#endregion

#region Overrides
protected override void CreateChildControls()
{
base.CreateChildControls();

switch (RenderType)
{
default: // default is hyperlink
case RenderTypes.LinkButton:

LinkButton link = new LinkButton();
link.TabIndex = -1;
link.Text = this.Text;
link.Attributes.Add("href",Page.GetPostBackClientH yperlink(this,string.Empty));
link.Click += new EventHandler(this.OnClick);
link.Command += new CommandEventHandler(this.OnCommand);

if(EnableConfirmation)
link.Attributes.Add("onclick","return " + CONFIRMFUNCTIONNAME + "();");

controlToRender = link;

break;
case RenderTypes.Button:

Button button = new Button();
button.Text = this.Text;
button.Click += new EventHandler(this.OnClick);
button.Command += new CommandEventHandler(this.OnCommand);

if(EnableConfirmation)
button.Attributes.Add("onclick","return " + CONFIRMFUNCTIONNAME +
"();" + Page.GetPostBackEventReference(this,string.Empty)) ;

controlToRender = button;

break;
}

//Copy control style attributes applied to this control
//to the control that will render
IEnumerator keys = this.Style.Keys.GetEnumerator();
while (keys.MoveNext())
{
string key = (string)keys.Current;
controlToRender.Style.Add(key, this.Style[key]);
}

//copy attributes applied to this control
//to the control that will render
IEnumerator attribKeys = this.Attributes.Keys.GetEnumerator();
while (attribKeys.MoveNext())
{
string key = (string)attribKeys.Current;
controlToRender.Attributes.Add(key, this.Attributes[key]);
}

if (this.CssClass != null && this.CssClass != string.Empty)
{
controlToRender.CssClass += " " + this.CssClass;
}

controlToRender.Enabled = this.Enabled;

controlToRender.ID = this.ID;
}

protected override void Render(HtmlTextWriter output)
{
EnsureChildControls(); //make sure child controls are created

//don't render this control, just the control chosen via
//the RenderType property
controlToRender.RenderControl(output);

}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);

//if enable confirmation then attach the
//confirm script to the page
if(EnableConfirmation)
{
RegisterConfirmationScript();
}
}

#endregion

#region Methods
private void RegisterConfirmationScript()
{
//only add the script to the page
//if it has not been added already
if(!Page.IsClientScriptBlockRegistered(CONFIRMSCRI PTKEY))
{
Page.RegisterClientScriptBlock(CONFIRMSCRIPTKEY,Cr eateJavaScriptFunction());
}
}

private string CreateJavaScriptFunction()
{
//build script string, the script is formatted for
//readability in view source
StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"javascript\">" + Environment.NewLine);
sb.Append("\tfunction " + CONFIRMFUNCTIONNAME + "(){" +
Environment.NewLine);
sb.Append("\t\tvar agree = confirm(\"" + ConfirmationText + "\");" +
Environment.NewLine + Environment.NewLine);
sb.Append("\t\tif(agree){" + Environment.NewLine);
sb.Append("\t\t\treturn true;" + Environment.NewLine);
sb.Append("\t\t}" + Environment.NewLine + "else{" + Environment.NewLine);
sb.Append("\t\t\treturn false;" + Environment.NewLine);
sb.Append("\t\t}" + Environment.NewLine);
sb.Append("\t}" + Environment.NewLine);
sb.Append("</script>" + Environment.NewLine);

return sb.ToString();
}

protected virtual void OnClick(object sender, EventArgs e)
{
if(Click != null)
Click(this,e);
}

protected virtual void OnCommand(object sender,CommandEventArgs e)
{
CommandEventHandler handler =
(CommandEventHandler)base.Events[ConfirmButton.EventCommand];

if(handler != null)
handler(this,e);

base.RaiseBubbleEvent(this,e);
}
#endregion

#region Properties
/// <summary>
/// Gets or sets the text for the control.
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue(""),Description("Gets or sets the text for the control")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

/// <summary>
/// Gets or sets the text for the confirmation message.
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("Are you sure you want to continue?"),Description("Gets or
sets the text for the confirmation message.")]
public string ConfirmationText
{
get
{
return confirmationText;
}

set
{
confirmationText = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether client-side confirmation is
enabled.
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue(true),Description("Gets or sets a value indicating whether
client-side confirmation is enabled.")]
public bool EnableConfirmation
{
get
{
return enableConfirmation;
}

set
{
enableConfirmation = value;
}
}

/// <summary>
/// Gets or sets a value indicating what type of button to render.
/// </summary>
[Bindable(true),
Category("Appearance"),
Description("Gets or sets a value indicating what type of button to
render.")]
public RenderTypes RenderType
{
get
{
return renderType;
}

set
{
renderType = value;
}
}

/// <summary>
/// Gets or sets the url for the LinkButton RenderType.
/// </summary>
[Bindable(true),
Category("Navigation"),
DefaultValue(""),Description("Gets or sets the url for the LinkButton
RenderType.")]
public string NavigateUrl
{
get
{
return navigateUrl;
}

set
{
navigateUrl = value;
}
}

/// <summary>
/// Gets or sets the command name associated with the ConfirmButton
control.
/// </summary>
[Category("Behavior"),
Description("Gets or sets the command name associated with the
ConfirmButton control."),
DefaultValue("")]
public string CommandName
{
get
{
return commandName;
}

set
{
commandName = value;
}
}

/// <summary>
/// Gets or sets an optional argument passed to the Command event handler
along with the associated CommandName property.
/// </summary>
[Bindable(true),
Category("Behavior"),
Description("Gets or sets an optional argument passed to the Command event
handler along with the associated CommandName property."),
DefaultValue("")]
public string CommandArgument
{
get
{
return commandArgument;
}

set
{
commandArgument = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether validation is performed when
the ConfirmButton control is clicked.
/// </summary>
[Bindable(false),
Category("Behavior"),
Description("Gets or sets a value indicating whether validation is
performed when the ConfirmButton control is clicked."),
DefaultValue(true)]
public bool CausesValidation
{
get
{
return causesValidation;
}

set
{
causesValidation = value;
}
}
#endregion

#region Events
/// <summary>
/// Occurs when ConfirmButton control is clicked.
/// </summary>
public event EventHandler Click;

/// <summary>
/// Occurs when the ConfirmButton control is clicked.
/// </summary>
public event CommandEventHandler Command
{
add
{
base.Events.AddHandler(ConfirmButton.EventCommand, value);
}
remove
{
base.Events.RemoveHandler(ConfirmButton.EventComma nd,value);
}
}
#endregion

#region IPostBackEventHandler Members

/// <summary>
/// Notifies the server control that caused the postback that it should
handle an incoming post back event.
/// </summary>
/// <param name="eventArgument">The post-back argument.</param>
public void RaisePostBackEvent(string eventArgument)
{
this.OnClick(this, new System.EventArgs());
this.OnCommand(this,new
CommandEventArgs(this.CommandName,this.CommandArgu ment));
}

#endregion

}
}

--
-Demetri
Jan 5 '06 #1
0 2926

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
I have some funky form/event behavior. Access 97. Split frontend/backend, using Access security. I have the same behavior (or lack of behavior) for the pag_Click() event of two separate pages...
1
by: Owatona | last post by:
Hi to all... I have 3 classes derived from webcontrol. I the first i have two dropdownlist, in the second i have some buttons, and the third its the webcontrol that are instanciate the two...
4
by: perspolis | last post by:
hi I manage a double click event in a combo box.. but this event doesn't fire ???? I don't know why??
1
by: Divya | last post by:
Hello This is my 1st project where I have to create a Webcontrol. I have created a simple custom control with a button and 2 labels added to a panel. My problem is that the event handler that I...
2
by: Novice | last post by:
I can't seem to get my Custom WebControl to output a button whose click event is associated with a particular method. Here is the code I have right now that contains a panel and there is a...
0
by: Novice | last post by:
Hey all, I've already posted this question and two posters offered suggestions and they worked - but now I would like to know why - and if possible the answer to a second question. Here is my...
2
by: =?Utf-8?B?Sm9yZ2UgUmliZWlybw==?= | last post by:
hello Inside a usercontrol i create a webcontrol button. I then render it onto a string on the usercontrol to built my UI I need to get access to that control Click event. To do so i try to bind...
0
by: JohnK82 | last post by:
Hi guys I have a webcontrol that displays an article in multiple parts. You can jump back and forward between the parts (I call them sheets) by clicking on a label. Each sheet has a label. The...
3
by: Tomasz J | last post by:
Hello Developers, How do create a custom WebControl (not UserControl), exposing OnClick event? I reviewed countless examples found by Google, but came across nothing helpful. Below I attach...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.