I have a custom CheckBox who implements the ICallbackEventHandler. At present, when the CheckBox is clicked, the checked value is passed through the ICallbackEventHandler interface, allowing me to capture the value on the server side.
Now, when I add this custom control to a page, everything seems to be working ok - when the checkbox is click, I get an alert indicating the checked state. The next thing I'm wanting to achieve is to be able to have several of these controls on one page.
The code I have above doesn't allow this as the client ID's are not passed to the CallServer client-side call.
Is anyone able to offer any advice as to how I can do this? I'm new to this whole AJAX malarky (I know, a bit late) but am slowly understanding.
I think since you are now creating one checkbox with AJAX functions, you can compile the control and install it in the ToolBar of your VS. Then drag and drop several times onto the page. That's all.
And you can also use another kind of customized control (ascx), just use the checkbox control inside something like Gridview or any kind of data presentation control, Thus you can have several ones.
kevinwebster...
None
0 Points
12 Posts
Custom CheckBox with AJAX Functionality
Jul 11, 2011 02:43 PM|LINK
Hi,
I have a custom CheckBox who implements the ICallbackEventHandler. At present, when the CheckBox is clicked, the checked value is passed through the ICallbackEventHandler interface, allowing me to capture the value on the server side.
Here is the code I have so far...
public class AjaxCheckBox : CheckBox, ICallbackEventHandler { private string _result; public AjaxCheckBox() { this.ClientIDMode = System.Web.UI.ClientIDMode.Static; } protected override void CreateChildControls() { ClientScriptManager cm = this.Page.ClientScript; string cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); StringBuilder sb = new StringBuilder(); sb.AppendLine("function CallServer(arg, context)");// { " + cbReference + "; }"); sb.AppendLine("{"); sb.AppendLine(" " + cbReference + ";"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("function ReceiveServerData(arg, context)");// { alert(eval(arg)); }"); sb.AppendLine("{"); sb.AppendLine(" alert(eval(arg));"); sb.AppendLine("}"); cm.RegisterClientScriptBlock(this.GetType(), "CallServer", sb.ToString(), true); this.Attributes.Add("onclick", "CallServer('" + this.ClientID + "', null)"); } public string GetCallbackResult() { return _result; } public void RaiseCallbackEvent(string eventArgument) { _result = "document.getElementById('" + this.ClientID + "').checked"; } }Now, when I add this custom control to a page, everything seems to be working ok - when the checkbox is click, I get an alert indicating the checked state. The next thing I'm wanting to achieve is to be able to have several of these controls on one page.
The code I have above doesn't allow this as the client ID's are not passed to the CallServer client-side call.
Is anyone able to offer any advice as to how I can do this? I'm new to this whole AJAX malarky (I know, a bit late) but am slowly understanding.
Thanks in advance. Kev
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Custom CheckBox with AJAX Functionality
Jul 13, 2011 02:41 AM|LINK
Hello OP:)
I think since you are now creating one checkbox with AJAX functions, you can compile the control and install it in the ToolBar of your VS. Then drag and drop several times onto the page. That's all.
And you can also use another kind of customized control (ascx), just use the checkbox control inside something like Gridview or any kind of data presentation control, Thus you can have several ones.
Thx