show-notice
hide-notice

Tuesday, 16 July 2013

How to use JavaScript by GetCallbackEventReference in asp.net


Introduction

I will explain How to use JavaScript by GetCallbackEventReference in asp.net.

Description

Examples of use Callback random numeric value that will set one up. When a user clicks a button on the program will send the form values from the random to show in the TextBox.



Example:

<%@ page language="C#" autoeventwireup="true" codefile="How-to-use-GetCallbackEventReference-c.aspx.cs"
    inherits="How_to_use_Callback_c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use GetCallbackEventReference</title>

    <script type="text/javascript">
                function GetNumber() {
                    UseCallback();
                }
                function GetRandomNumberFromServer(txtGetNumber, context) {
                    document.forms[0].txtGetNumber.value = txtGetNumber
                }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="Get Random Number" onclick="GetNumber()" />
        <br />
        <br />
        <asp:textbox id="txtGetNumber" runat="server"></asp:textbox>
    </div>
    </form>
</body>
</html>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;


public partial class How_to_use_Callback_c : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    private string CallbackResult = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetRandomNumberFromServer", "context");
        string cbScript = "function UseCallback(arg, context)" + "{" + cbReference + ";" + "}";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cbScript, true);
    }
    public void RaiseCallbackEvent(string eventArgument)
    {
        Random rnd = new Random();
        CallbackResult = rnd.Next().ToString();
    }
    public string GetCallbackResult()
    {
        return CallbackResult;
    }
}

 
 

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com