CallbackEventHandle: Page.ClientScript

Objectives:

  • System.Web.UI.Ir

Step: 1

Create an new Web-Site or Web-Application, add and edit the existing code, using the script given in the code description.

Step : 2 Code Text

Default.aspx

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

<!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 id="Head1" runat="server">
<title>mm.IsCallBack 2</title>
<script type="text/javascript">
function ReceiveServerData(arg, context) {
var doc = document.getElementById("content");
var t1 = document.getElementById("T1");
var msg = doc.innerHTML + t1.value;
ResultsSpan.innerHTML = "<font color='red'>Processed callback. Without using IsPostBack.</font> <br/>" +msg;
}
</script>
<style type="text/css">
#Text1  {  width: 224px;  }
.style1  {  width: 120px;   }
.style2   {  }
#T1    {  width: 262px;  }
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="padding: 10px; background-color: #FFFFCC; border: 2px solid #800000; width: 477px;">
<div id="content" style="padding: 10px; margin: 10px 1px 10px 10px; border: medium dotted #800000; width: 414px;">
This example uses Page.ClientScript IsClientScriptBlockRegistered </div>
<a href="#" onclick="CallServer(); return false;">&nbsp;</a>&nbsp
<br />
<table style="width:100%;">
<tr> <td class="style1">
<a href="#" onclick="CallServer(); return false;">Call Back</a></td>
<td class="style2">
<a href="#" onclick="CallServer(alert('My-Message')); return false;">Call Back Alert</a></td>
<td>
<asp:Button ID="Button1" runat="server" Text="IsPostBack-Test"
onclick="Button1_Click" PostBackUrl="~/Default.aspx"/>
</td>
</tr>
<tr><td class="style1">Type a Message</td><td class="style2" colspan="2">
<input id="T1" type="text" /></td></tr>
</table>
<asp:Label ID="L1" runat="server" Text="Label "></asp:Label>
<br /><br />&nbsp;<br />
Call Back Out-Put : <span id="ResultsSpan" runat="server"></span>
<br />
</div>
</form>
</body>
</html>
 

Defaut.aspx.cs

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

public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler

{
// use LisDictionary object to create a list of items
protected string returnValue; private static int n1 = 0;
protected void Page_Load(object sender, EventArgs e)
{
L1.Text += "Page-Load()" + n1 + "<br/>";
ClientScriptManager cm = Page.ClientScript;
String cbRef = cm.GetCallbackEventReference(this, "arg","ReceiveServerData", "");
String cbScript = "function CallServer(arg, context) {" +cbRef + "; }";
cm.RegisterClientScriptBlock(this.GetType(),"CallServer", cbScript, true);

n1++;
}
// these two methods must be added
public void RaiseCallbackEvent(String eventArgument)
{

}
public String GetCallbackResult()
{
return returnValue;
}
 protected void Button1_Click(object sender, EventArgs e)
{
L1.Text += "Button Fired &nbsp: and ";
}
protected void Page_Init(object sender, EventArgs e)
{
L1.Text += "<br/> Page_Init() Fired";
}

 

Step 3: Runtime analysis

First Time Load shows n1 = 0;

Test CallBack, and note that 'innerHTML" will understand HTML script.

Now, if you click on the Button, since n1 was a static integer, it will post you how many round of Client-Script Manager traversed this route with Page.ClientScript.