Help!! Event handler fires everytime I refresh the page!
-
Hello friends Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser ;-) to dynamically add/delete rows containing two values: Role and Deployable. So I thought GridView of course. Also I figured the best way to do this is to create a dataset and store it as session variable, and dynamically bind that sucker to my GridView… below is my ‘example’ code: ********Asp net Page*******
Doctor Nurse Engineer Teacher Clerk Yes No
Delete *******Code Behind****** using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class TEST_testingGridView : System.Web.UI.Page { DataSet ds; DataTable dt; DataRow dr; StringBuilder sb; protected void Page_Load(object sender, EventArgs e) { if (Session["smeRoles"] != null) { GridView2.DataSource = (DataSet)(Session["smeRoles"]); GridView2.DataBind(); } } protected void addButton_C -
Hello friends Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser ;-) to dynamically add/delete rows containing two values: Role and Deployable. So I thought GridView of course. Also I figured the best way to do this is to create a dataset and store it as session variable, and dynamically bind that sucker to my GridView… below is my ‘example’ code: ********Asp net Page*******
Doctor Nurse Engineer Teacher Clerk Yes No
Delete *******Code Behind****** using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class TEST_testingGridView : System.Web.UI.Page { DataSet ds; DataTable dt; DataRow dr; StringBuilder sb; protected void Page_Load(object sender, EventArgs e) { if (Session["smeRoles"] != null) { GridView2.DataSource = (DataSet)(Session["smeRoles"]); GridView2.DataBind(); } } protected void addButton_CFirst thing I'd do is put IsPostback in the Page_Load event. Don't bind based on the session.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before. -
Hello friends Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser ;-) to dynamically add/delete rows containing two values: Role and Deployable. So I thought GridView of course. Also I figured the best way to do this is to create a dataset and store it as session variable, and dynamically bind that sucker to my GridView… below is my ‘example’ code: ********Asp net Page*******
Doctor Nurse Engineer Teacher Clerk Yes No
Delete *******Code Behind****** using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class TEST_testingGridView : System.Web.UI.Page { DataSet ds; DataTable dt; DataRow dr; StringBuilder sb; protected void Page_Load(object sender, EventArgs e) { if (Session["smeRoles"] != null) { GridView2.DataSource = (DataSet)(Session["smeRoles"]); GridView2.DataBind(); } } protected void addButton_CI'm going to guess that the event handler for the addRoles button (or is it addButton Button?) fires when you refresh the page only if you press the button before refreshing the page (meaning on the previous page view, the button was pressed). If this is correct: This is a common problem for page developers. When you hit refresh, the posted data is posted back; that posted data contained information telling the page to execute the button's event handler so when you hit refresh the event will be fired again. There are several ways to overcome this, the simplest being to check the datasource to see if the data being inserted is already in the datasource, if not insert it, if so don't insert it.
-
I'm going to guess that the event handler for the addRoles button (or is it addButton Button?) fires when you refresh the page only if you press the button before refreshing the page (meaning on the previous page view, the button was pressed). If this is correct: This is a common problem for page developers. When you hit refresh, the posted data is posted back; that posted data contained information telling the page to execute the button's event handler so when you hit refresh the event will be fired again. There are several ways to overcome this, the simplest being to check the datasource to see if the data being inserted is already in the datasource, if not insert it, if so don't insert it.
Thanks Paszat :-) Although, I have to say, when I trigger some other event handler and then refresh the page data persists. But anyways- I will give your solution a try. Thanks again.
Nila
-
Hello friends Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser ;-) to dynamically add/delete rows containing two values: Role and Deployable. So I thought GridView of course. Also I figured the best way to do this is to create a dataset and store it as session variable, and dynamically bind that sucker to my GridView… below is my ‘example’ code: ********Asp net Page*******
Doctor Nurse Engineer Teacher Clerk Yes No
Delete *******Code Behind****** using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class TEST_testingGridView : System.Web.UI.Page { DataSet ds; DataTable dt; DataRow dr; StringBuilder sb; protected void Page_Load(object sender, EventArgs e) { if (Session["smeRoles"] != null) { GridView2.DataSource = (DataSet)(Session["smeRoles"]); GridView2.DataBind(); } } protected void addButton_CHi, I feel you are Missing some thing else. By Default any Event will not fire unless it is called implicitly. Here i want some info from you whether you debugged this with break points if so which event is called first?.
-
Hello friends Ok, so I was tasked to build this little ‘window’ (I’m speaking enduser ;-) to dynamically add/delete rows containing two values: Role and Deployable. So I thought GridView of course. Also I figured the best way to do this is to create a dataset and store it as session variable, and dynamically bind that sucker to my GridView… below is my ‘example’ code: ********Asp net Page*******
Doctor Nurse Engineer Teacher Clerk Yes No
Delete *******Code Behind****** using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class TEST_testingGridView : System.Web.UI.Page { DataSet ds; DataTable dt; DataRow dr; StringBuilder sb; protected void Page_Load(object sender, EventArgs e) { if (Session["smeRoles"] != null) { GridView2.DataSource = (DataSet)(Session["smeRoles"]); GridView2.DataBind(); } } protected void addButton_Cjust put and change according to ur need following code into
public partial class { private bool _refreshState; //define bool type variable which return to Refresh state in Internet Explorer private bool _isRefresh; //define bool type variable which return wheather Refresh click or not in Internet Explorer public bool IsRefresh // { get { return _isRefresh;//get the value of Refresh click from Internet Explorer } } protected override object SaveViewState() { Session["_ISREFRESH"] = _refreshState; //define Session variable and intilize object[] allstates = new object[2]; // Define object array named allstates allstates[0] = base.SaveViewState(); //get the base.SaveViewState in allstate array at 0 position allstates[1] = !_refreshState; //get the allstate array at 1 position from !_refreshState return allstates; } protected override void LoadViewState(object savedState)//function for LoadViewState { try { object[] allstates = (object[])savedState; //Parsing saveState value at allsataes object array base.LoadViewState(allstates[0]); //Loading viewState value _refreshState = (bool)allstates[1]; _isRefresh = _refreshState == (bool)Session["_ISREFRESH"]; } catch (Exception e) { } } }
and which event is called thru Refresh just put flowwowing code intoif (_isRefresh == false) { your code for event is here }
hope tht'll helps u :)Deepak Smile a Lots,Its Costs Nothing
-
just put and change according to ur need following code into
public partial class { private bool _refreshState; //define bool type variable which return to Refresh state in Internet Explorer private bool _isRefresh; //define bool type variable which return wheather Refresh click or not in Internet Explorer public bool IsRefresh // { get { return _isRefresh;//get the value of Refresh click from Internet Explorer } } protected override object SaveViewState() { Session["_ISREFRESH"] = _refreshState; //define Session variable and intilize object[] allstates = new object[2]; // Define object array named allstates allstates[0] = base.SaveViewState(); //get the base.SaveViewState in allstate array at 0 position allstates[1] = !_refreshState; //get the allstate array at 1 position from !_refreshState return allstates; } protected override void LoadViewState(object savedState)//function for LoadViewState { try { object[] allstates = (object[])savedState; //Parsing saveState value at allsataes object array base.LoadViewState(allstates[0]); //Loading viewState value _refreshState = (bool)allstates[1]; _isRefresh = _refreshState == (bool)Session["_ISREFRESH"]; } catch (Exception e) { } } }
and which event is called thru Refresh just put flowwowing code intoif (_isRefresh == false) { your code for event is here }
hope tht'll helps u :)Deepak Smile a Lots,Its Costs Nothing
Thanks Deepak! There has got to be a bug in this, because the button click event fires all on its own when I refresh the page... so I finally decided to create a small private method that checks to see whether or not events in the button click would 'quilify' to be executed - very similar to your approach. Thanks again, don't know what I would do without all you guys' help and support!!!!
Nila
-
Hi, I feel you are Missing some thing else. By Default any Event will not fire unless it is called implicitly. Here i want some info from you whether you debugged this with break points if so which event is called first?.
Nagendra, Thanks for the debug tip. After stepping into the code, I saw that the button click event fires again when I refresh the page... very frustrating!! So to remedy it as quickly as possible, I created a small method that would check to see if the button click event would 'quilify' to be fired given user's input. I just don't have the time to investigate this silliness- need to keep the boss happy ;P Thanks again!
Nila
-
Nagendra, Thanks for the debug tip. After stepping into the code, I saw that the button click event fires again when I refresh the page... very frustrating!! So to remedy it as quickly as possible, I created a small method that would check to see if the button click event would 'quilify' to be fired given user's input. I just don't have the time to investigate this silliness- need to keep the boss happy ;P Thanks again!
Nila
Hai, That is a Good Method of Making your self Comfortable in Office. But you try it later and you can come across some good concepts in that area. and if possible send me that page complete as an attachment to nagendrarao@glowtouch.com.
-
Hai, That is a Good Method of Making your self Comfortable in Office. But you try it later and you can come across some good concepts in that area. and if possible send me that page complete as an attachment to nagendrarao@glowtouch.com.
It's not about making myself comfortable; Im just trying to remedy this and move on- there is lots of pressure when you develop for an emergency operations type group. And I agree with you on finding out exactly WHY sometimes things behave the way they do :-) But now is not the time for that, maybe later. Let me do a couple of things, and then I will send you the code. Thanks again!
Nila