 | Callback ICallbackEventHandler implementation |
| using System;
| | 1:using System.Web.UI ;
| | 2:namespace Arindam
| | 3:{
| | 4: public class Callback_page : System.Web.UI.Page, ICallbackEventHandler
| | 5: {
| | 6:
| | 7: protected void Page_Load(object sender, EventArgs e)
| | 8: {
| | 9: if (!IsPostBack)
| | 10: {
| | 11: SetCallBackBehaviour();
| | 12: }
| | 13: }
| | 14:
| | 15: #region ICallbackEventHandler Members
| | 16:
| | 17: public string GetCallbackResult()
| | 18: {
| | 19: return _callbackResult;
| | 20: }
| | 21:
| | 22: public void RaiseCallbackEvent(string eventArgument)
| | 23: {
| | 24: _callbackResult = DateTime.Now.ToLongTimeString();
| | 25: }
| | 26:
| | 27:
| | 28: void SetCallBackBehaviour()
| | 29: {
| | 30: ParentOrganizations.DataSource = GetOrganisation();//get data from database
| | 31: ParentOrganizations.DataTextField = "Name";
| | 32: ParentOrganizations.DataValueField = "OrganizationId";
| | 33: ParentOrganizations.DataBind();
| | 34: ParentOrganizations.Items.Insert(0, new ListItem("Select", "0"));
| | 35:
| | 36: ParentOrganizations.Attributes.Add("onchange", "GetChildren(this.options[this.selectedIndex].value, 'ddl');");
| | 37: string callBack = Page.ClientScript.GetCallbackEventReference(this, "arg", "ClientCallback", "context", true);
| | 38: string clientFunction = "function GetChildren(arg, context){ " + callBack + "; }";
| | 39: Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetChildren", clientFunction, true);
| | 40:
| | 41: }
| | 42:
| | 43: #endregion
| | 44: }
| | 45:}
| | 46: |
|
 | Callback Client script |
| <asp:DropDownList ID="ParentOrganizations" Runat="server" />
| | 1: <asp:DropDownList ID="ChildOrganizations" Runat="Server" />
| | 2:
| | 3: <script language="javascript" type="text/javascript" >
| | 4: function ClientCallback(result, context){
| | 5: alert(result);
| | 6: alert(context);
| | 7: var childOrganizations = document.forms[0].elements['<%=ChildOrganizations.UniqueID%>'];
| | 8: if (!childOrganizations){
| | 9: return;
| | 10: }
| | 11: childOrganizations.length = 0;
| | 12: if (!result){
| | 13: return;
| | 14: }
| | 15:
| | 16: var rows = result.split('|');
| | 17: for (var i = 0; i < rows.length; ++i){
| | 18: var values = rows[i].split('^');
| | 19: var option = document.createElement("OPTION");
| | 20: option.value = values[0];
| | 21: option.innerHTML = values[1];
| | 22: childOrganizations.appendChild(option);
| | 23: }
| | 24: }
| | 25:
| | 26: function ClientCallbackError(result, context){
| | 27: alert(result);
| | 28: }
| | 29: </script>
| | 30: |
|