function onPostBack(){ var y=generateRandomSequence(); var hdnGuid=document.getElementById("hdnGuid"); hdnGuid.value=y;}
function generateRandomSequence(){ var g = ""; for(var i = 0; i < 32; i++) g += Math.floor(Math.random() * 0xF).toString(0xF) return g;}
The above javascript function is referred in a JS files and called on the button submit click as shown in the below HTML code snippet.
<title>Untitled Page</title><script type="text/javascript" language="javascript" src="Client-Side_Validn.js" temp_src="Client-Side_Validn.js"></script></script></head><body><form id="form1" runat="server" onsubmit="onPostBack()"><asp:Button ID="Button1" runat="server" Text="Button" /></form></body></html>
HttpModule code
The next important code is the 'HttpModule' code. As a first step let's create a simple GUID class which help us store the GUID values as shown in the below figure.
public class GuidClass{ public GuidClass() { // // TODO: Add constructor logic here // } private string guid; public string Guid { get { return guid; } set { guid = value; } }}The next step is to create a simple 'HttpModule' class which overrides the 'page_Init' event and the 'page_Load' event.
public class GuidClass{ public GuidClass() { // // TODO: Add constructor logic here // } private string guid; public string Guid { get { return guid; } set { guid = value; } }}
The next step is to create a simple 'HttpModule' class which overrides the 'page_Init' event and the 'page_Load' event.
void _page_Init(object sender, EventArgs e){ HiddenField hdnGuid = new HiddenField(); hdnGuid.ID = "hdnGuid"; if (!_page.IsPostBack) hdnGuid.Value = Guid.NewGuid().ToString(); _page.Form.Controls.Add(hdnGuid);}
In the 'page_Load' event we check if the hidden field value is same as the old value. In case the value is not same that means it's a 'postback' and if the value is same then its 'refresh'. As per situation we set the 'httpContent.Items["Refresh"]' value.
void _page_Load(object sender, EventArgs e){ HiddenField h1 = (HiddenField)(_page.Form.FindControl("hdnGuid")); GuidClass currentGuid =new GuidClass(); currentGuid.Guid= h1.Value; System.Web.HttpContext _httpContext = System.Web.HttpContext.Current; if (temp.Contains<string>(currentGuid.Guid)) { _httpContext.Items.Add("IsRefresh",true); } else { if(!(currentGuid.Guid.Equals(null)||currentGuid.Guid.Equals(""))) temp.Enqueue(currentGuid.Guid); _httpContext.Items.Add("IsRefresh",false); }}We also need to ensure that the handler is registered in the 'httpModules' tag. <httpModules> <add name="Myhandler" type="Myhandler"/> </httpModules>
void _page_Load(object sender, EventArgs e){ HiddenField h1 = (HiddenField)(_page.Form.FindControl("hdnGuid")); GuidClass currentGuid =new GuidClass(); currentGuid.Guid= h1.Value; System.Web.HttpContext _httpContext = System.Web.HttpContext.Current; if (temp.Contains<string>(currentGuid.Guid)) { _httpContext.Items.Add("IsRefresh",true); } else { if(!(currentGuid.Guid.Equals(null)||currentGuid.Guid.Equals(""))) temp.Enqueue(currentGuid.Guid); _httpContext.Items.Add("IsRefresh",false); }}
We also need to ensure that the handler is registered in the 'httpModules' tag.
<httpModules> <add name="Myhandler" type="Myhandler"/> </httpModules>
if ((bool)HttpContext.Current.Items["IsRefresh"]){ Response.Write("refreshed");}else{ Response.Write("Postback");}
Source Code
You can download the course code from top of this article.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: