.ASPX Code ...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_ClientToServerPostback.aspx.cs"
Inherits="test_ClientToServerPostback" %>
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>Calling Server-Side Method from Client-Side Javascript.title>
<script type="text/javascript">
function LookUp()
{
var lbl = document.getElementById("TextBox1");
CallServerMethod(lbl.value, "");
}
function ReturnedServerData(retValue)
{
document.getElementById("Label1").innerHTML = retValue;
}
script>
head>
<body>
<form id="form1" runat="server">
<div>
Try Writing : accept/reject <br/> and see the result.<br />
<asp:TextBox ID="TextBox1" runat="server">asp:TextBox><br />
<a href="#" onclick="LookUp();">ClickMea><br />
<br />
<asp:Label ID="Label1" runat="server">asp:Label>
div>
form>
body>
html>
===============================
.CS Code ...
using System;
using System.Web.UI;
public partial class test_ClientToServerPostback : System.Web.UI.Page, ICallbackEventHandler
{
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReturnedServerData", "context");
String callbackScript = "function CallServerMethod(arg, context)" + "{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServerMethod", callbackScript, true);
}
}
{
if (eventArgument.Trim() == String.Empty)
{
returnValue = "Sorry, please write somthing in textbox.";
}
else
{
if (eventArgument == "accept")
{
returnValue = "Congrats, for accepting!";
}
else if (eventArgument == "reject")
{
returnValue = "You have succesfully rejected !";
}
else
{
returnValue = "You have entered : " + eventArgument + ".";
}
}
}
{
return returnValue;
}
}
-
ShriKrishna Bhardwaj , With ASP.Net, SQL Server, Javascript
1 comment:
Very nice article.. Man keep it up..
Post a Comment