ICallbackEventHandler in asp.net
//Code
//Form
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page,ICallbackEventHandler
{
public string results = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
string strScript = string.Format(@"function ServerRequest(arg,ctx) {{ {0}; }}
", Page.ClientScript.GetCallbackEventReference(this, "arg", "GetResult", "ctx", "GetError", false));
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallbackFunctions", strScript, true);
}
#region ICallbackEventHandler Members
public string GetCallbackResult()
{
return results;
}
public void RaiseCallbackEvent(string eventArgument)
{
results = "false";
}
#endregion
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("hello");
}
}
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page,ICallbackEventHandler
{
public string results = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
string strScript = string.Format(@"function ServerRequest(arg,ctx) {{ {0}; }}
", Page.ClientScript.GetCallbackEventReference(this, "arg", "GetResult", "ctx", "GetError", false));
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallbackFunctions", strScript, true);
}
#region ICallbackEventHandler Members
public string GetCallbackResult()
{
return results;
}
public void RaiseCallbackEvent(string eventArgument)
{
results = "false";
}
#endregion
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("hello");
}
}
//Form
<html>
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var Mark=false;
function CheckJsValidate()
{
if(Page_Validators[0].isvalid)
{
document.getElementById("DivSecurity").style.display="block";
}
else{
document.getElementById("DivSecurity").style.display="none";
}
}
//Check if the user answered his question correctly
function CheckAnquestion()
{
var varDrop = document.getElementById("DropDownList1").value;
var varTxt = document.getElementById("TextBox3").value;
ServerRequest([varDrop,varTxt],"")
return false;
}
//If success,Submit the current form
function GetResult(result, context) {
if(eval(result))
{
document.getElementById("Button1").click();
}else
alert('Your answer is wrong!')
}
//If false,alert the error message
function GetError(result, context) {
alert(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" onblur="CheckJsValidate()"></asp:TextBox>
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox1" ControlToCompare="TextBox2"
ErrorMessage="CompareValidator"></asp:CompareValidator>
<div id="DivSecurity" style="display:none">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="Question1">Question1</asp:ListItem>
<asp:ListItem Value="Question2">Question2</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
<div id="DivResult"></div>
</div>
<asp:Button ID="Button1" runat="server" Text="Submit"
OnClientClick="javascript:return CheckAnquestion();" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var Mark=false;
function CheckJsValidate()
{
if(Page_Validators[0].isvalid)
{
document.getElementById("DivSecurity").style.display="block";
}
else{
document.getElementById("DivSecurity").style.display="none";
}
}
//Check if the user answered his question correctly
function CheckAnquestion()
{
var varDrop = document.getElementById("DropDownList1").value;
var varTxt = document.getElementById("TextBox3").value;
ServerRequest([varDrop,varTxt],"")
return false;
}
//If success,Submit the current form
function GetResult(result, context) {
if(eval(result))
{
document.getElementById("Button1").click();
}else
alert('Your answer is wrong!')
}
//If false,alert the error message
function GetError(result, context) {
alert(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" onblur="CheckJsValidate()"></asp:TextBox>
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox1" ControlToCompare="TextBox2"
ErrorMessage="CompareValidator"></asp:CompareValidator>
<div id="DivSecurity" style="display:none">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="Question1">Question1</asp:ListItem>
<asp:ListItem Value="Question2">Question2</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
<div id="DivResult"></div>
</div>
<asp:Button ID="Button1" runat="server" Text="Submit"
OnClientClick="javascript:return CheckAnquestion();" onclick="Button1_Click" />
</div>
</form>
</body>
</html>