Click here to Skip to main content
15,920,053 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How i can crerate a ajax post back on servercontrol?i find this code to create ajax Postback on ClientControl.I want use a server control Button instead of "LookUpButton" client control?

ASP.NET
<head id="Head1"  runat="server">
  <title>Client Callback Example</title>
  <script type="text/ecmascript">
      function LookUpStock() {
          var lb = document.getElementById("ListBox1");
          try 
          {
            var product = lb.options[lb.selectedIndex].text;
            CallServer(product, "");
          }
          catch(err)
          {
            alert("Please select a product.");
          }
      }

      function ReceiveServerData(rValue) {
          document.getElementById("ResultsSpan").innerHTML = rValue;

      }
  </script>
</head>
<body>
  <form id="form1"  runat="server">
    <div>
      <asp:ListBox ID="ListBox1" Runat="server">
      <br />
      <br />
      <input type="button" value="Look Up Stock" id="LookUpButton"  önclick="LookUpStock()" />
      <br />
      <br />
      Items in stock: <span id="ResultsSpan"  runat="server">
      <br />
    </div>
  </form>
</body>



C#
public partial class ClientCallback : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
    protected System.Collections.Specialized.ListDictionary catalog;
    protected String returnValue;
    protected void Page_Load(object sender, EventArgs e)
    {
     
        string cbReference =
            Page.ClientScript.GetCallbackEventReference(this,          
            "arg","ReceiveServerData","context");                      
        string callbackScript;
        callbackScript = "function CallServer(arg, context)" +
            "{ " + cbReference + ";}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
            "CallServer", callbackScript, true);

        catalog = new System.Collections.Specialized.ListDictionary();
        catalog.Add("monitor", 12);
        catalog.Add("laptop", 10);
        catalog.Add("keyboard", 23);
        catalog.Add("mouse", 17);

   
        ListBox1.DataSource = catalog;
        ListBox1.DataTextField = "key";
        ListBox1.DataBind();

    }

    public void RaiseCallbackEvent(String eventArgument)
    {
        if (catalog[eventArgument] == null)
        {
            returnValue = "-1";
        }
        else
        {
            returnValue = catalog[eventArgument].ToString();
        }
    }


    public String GetCallbackResult()
    {
        return returnValue;
    }
}
Posted
Updated 19-Aug-11 14:48pm
v3

You can call JS function on server control like this,use this on PageLoad of the page

C#
ControlID.Attributes.Add("javascript: onclick,"return LookUpStock()");
 
Share this answer
 
v2
Assign Javascript function "LookUpStock()" to "OnClientClick" event of Server Control as below.

VB
<asp:Button id="btnTest" OnClick="btnTest_OnClick" OnClientClick="LookUpStock()"
Text="Test" runat="server" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900