Search content within the blog

Thursday, November 13, 2008

Calling serverside code when closing browser

ASPX.CS Contents

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] == "FromJavascript")
{
MyFuntion();
}
}

public void MyFuntion()
{
Response.Write("test write from server");
}

ASPX Contents


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript">
function PerformServerTasks()
{
__doPostBack("FromJavascript","");
alert("Form Unloading. After you click ok to this message.Server Side code will work");
}
</script>
</head>
<body onunload="PerformServerTasks();">
<form id="form1" runat="server">
<div>
<h1>Test Page</h1>
<%-- <a href="#" onclick="PerformServerTasks();">s</a>--%>
<input id="Text1" type="text" runat="server" value="" />
<asp:TextBox ID="SDF" runat="server" AutoPostBack="True" Text="Dont worry about this textbox.But dont remove it"></asp:TextBox>
</div>
</form>
</body>
</html>

No comments:

Post a Comment