Thursday, August 2, 2012

Client Callbacks Example in Asp.net

Client callbacks : Rather than posting back the entire page, your custom control can send a request to the server to get just the additional information it needs.

Example:                                                        On Click of Button show Alert box with text value.

 in .aspx page                                                             <script type="text/javascript">
        function ss() {
            var va = document.forms[0].Text1.value;
            UseCallback(va, "");
      
    
        }
        function show(Text1, context) {
            alert(Text1);
      
        }
    </script> 
      <asp:Panel ID="Panel2" runat="server">
      <input id="Text1" type="text" runat="server" />
      <input id="Button1" type="button" value="button" onclick="ss()"/>
        </asp:Panel>

   in .cs page                              
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
  
 public partial class _Default: System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
  protected void Page_Load(object sender, EventArgs e)
    {
        string cref = Page.ClientScript.GetCallbackEventReference(this, "arg", "show", "context");
        string cscript = "function UseCallback(arg,context)" +
            "{" + cref + ";" + "}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cscript, true);
    } 
  public string GetCallbackResult()
    {
        return aa;
    }
    public void RaiseCallbackEvent(string eventArgument)
    {
        aa = eventArgument;
    } 
}
OutPut
 

No comments:

Post a Comment