Sunday, 30 September 2012

Java script Interaction with server in ASP.NET

java script is a alanguage which runs on client machine
but we can interact with server in
Server returns data and it catch by java script functions and rebder
data as you have hanldle it.Its prevent full page paost back..My example will show
random number in textbox generated by server.

.aspx page code..
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>Communication With Server</title>
    <script language="javascript">
        function GetNumber()
        function GetRandomNumberFromServer(TextBox1,context)
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br /><br />
        <input type="button" value="Get Random Number.." onclick="GetNumber()" />

Server Side Code..

public partial class _Default : 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);
    protected void Button1_Click(object sender, EventArgs e)

    public void RaiseCallbackEvent(string eventArg)
        Random rnd = new Random();
        _callbackResult = rnd.Next().ToString();
    public string GetCallbackResult()
        return _callbackResult;

1 comment:

  1. informative post about java give lot of information which is helpful for me.thanks for this post