I have a GridView, where I dynamically add checkbox controls to a particular cell, all the way down an entire column. It's a replacement of the AutoGenerated "Select" LinkButton.
My "extended" GridView control has the open for Single or Multi select. The problem I have is in "Single" selection mode. When a user clicks on a Checkbox, all the other Checkboxes should clear out their values, but they don't.
Each Checkbox is set to AutoPostBack = True, and in doing so, I have to re-create all the Checkboxes again, which is fine. What I find perplexing is that after I re-create all my Checkboxes and add them where needed, in code they all have .Checked = False,
but when the page finishes refreshing from the PostBack, the previous Checked values are still there, persisting even though I forcably recreated each Checkbox at the time of PostBack.
Why are these Checkboxes retaining their values? I would expect that they would have started fresh on each re-create. How do I clear their values?
Note: I have .EnableViewState = False set on each checkbox control at the time of creation.
At what point in the page lifecycle are you adding the CheckBox controls? Controls get their postback values set before OnLoad is called. If you add your controls after that point, they won't get their values.
At what point in the page lifecycle are you adding the CheckBox controls? Controls get their postback values set before OnLoad is called. If you add your controls after that point, they won't get their values.
The controls are being created by a method I created called "PreFormatGrid", which is called in the controls Load() event.
That's the problem. You need to add the controls during OnInit.
That's not an option. The Checkboxes need to have unique ID's, and the ID names I create for each, encode a RecordID number inside it (ie:chkSelect_25805). Since Checkboxes don't offer a value submit property, this was the only way I could think of getting
what I needed. I parse the name of the Checkbox at the time of PostBack to know which record it's referring to. The issue is, I build the Checkbox name dynamically (by use of a property that defines the PrimaryKey column name of the DataSource being bound
to). At the time of the Init() event, the property I need has no value yet (don't know when it gets populated, all I know is by the time the Load() event execute, the Property I need now has a value), causing the creation method to crash.
Would you know what the IDs are supposed to be by the time LoadPostbackData is called? You could override that method and set the values of the checkboxes there. You could set the values anywhere, really, when you know the ID you're looking for. All the
postdata is available in Request.Form(or Request.Params), you'd be able to get the values that way, too.
Would you know what the IDs are supposed to be by the time LoadPostbackData is called? You could override that method and set the values of the checkboxes there.
I don't know where the LoadPostBackData() event is, or how to "trap & step through" it in order to give you an answer. Is it a page event, or a control event? How do I hook into it? I tried looking for it (must be a hidden event), and I tried Googling...can't
really seem to get an answer to where it is...
rossisdead2
You could set the values anywhere, really, when you know the ID you're looking for. All the postdata is available in Request.Form(or Request.Params), you'd be able to get the values that way, too.
On a side note, I know I can set the value anywhere...the problem is that MY value (.Checked = FALSE) is overwritten by the value of the checkbox submitted at the time of the PostBack (.Checked = TRUE). I honestly don't care what the values are of the Checkboxes
at all, I only care about the Postback event and the Checkbox control's ID. It's value I want to discard completely. Problem is I can't seem to figure out how...
I don't know where the LoadPostBackData() event is, or how to "trap & step through" it in order to give you an answer. Is it a page event, or a control event? How do I hook into it? I tried looking for it (must be a hidden event), and I tried Googling...can't
really seem to get an answer to where it is...
LoadPostBackData's an overridable method on a control, not an event, sorry.
BSantiag
On a side note, I know I can set the value anywhere...the problem is that MY value (.Checked = FALSE) is overwritten by the value of the checkbox submitted at the time of the PostBack (.Checked = TRUE). I honestly don't care what the values are of the Checkboxes
at all, I only care about the Postback event and the Checkbox control's ID. It's value I want to discard completely. Problem is I can't seem to figure out how...
Right, what I meant is that Request.Form has all the raw posted back values. Since your checkboxes are being created during OnLoad, they don't get to have their values set by LoadPostBackData, which occurs before the OnLoad event fires. You *can* however
get their value from Request.Form as long as you know their UniqueID. Request.Form["some$control$name"] will either return "true" if the checkbox was checked, or null if it wasn't.
LoadPostBackData's an overridable method on a control, not an event, sorry.
Sorry, still don't understand how I can hook into this Method, event or not. I can't find it. Visual Studio just complains that LoadPostBackData() doesn't exist.
rossisdead2
Right, what I meant is that Request.Form has all the raw posted back values. Since your checkboxes are being created after the LoadPostBackData method has been called on all the other controls, they don't get to participate in that. You *can* however get
their value from Request.Form as long as you know their UniqueID. Request.Form["some$control$name"] will either return "true" if the checkbox was checked, or null if it wasn't.
Well, only the Checkbox Control that caused the PostBack is listed in the Request.Forms collection, the other controls are not listed. This is part of my confusion and frustration. The values don't even exist in the Request.Forms collection, so how they
get to "retain" their value is confusing me. I did read up that even disabling ViewState doesn't help because controls have an additional ControlState that cannot under any circumstances be disabled, and of course this information doesn't show up in the Request.Forms
collection. But like I stated, I don't care about the values. I don't want them at all. I want them gone, cleared, deleted, removed. I do not want the Checkbox control to remember it's previous value...I want it to render after PostBack clean, unchecked.
I never would have guessed it would be so difficult to do this...
BSantiag
Member
8 Points
5 Posts
Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 03:53 PM|LINK
I have a GridView, where I dynamically add checkbox controls to a particular cell, all the way down an entire column. It's a replacement of the AutoGenerated "Select" LinkButton.
My "extended" GridView control has the open for Single or Multi select. The problem I have is in "Single" selection mode. When a user clicks on a Checkbox, all the other Checkboxes should clear out their values, but they don't.
Each Checkbox is set to AutoPostBack = True, and in doing so, I have to re-create all the Checkboxes again, which is fine. What I find perplexing is that after I re-create all my Checkboxes and add them where needed, in code they all have .Checked = False, but when the page finishes refreshing from the PostBack, the previous Checked values are still there, persisting even though I forcably recreated each Checkbox at the time of PostBack.
Why are these Checkboxes retaining their values? I would expect that they would have started fresh on each re-create. How do I clear their values?
Note: I have .EnableViewState = False set on each checkbox control at the time of creation.
</div>asp.net Postback checkbox
Programmer Analyst
(SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS, Cognos ReportNet)
rossisdead2
Participant
1313 Points
300 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 04:21 PM|LINK
At what point in the page lifecycle are you adding the CheckBox controls? Controls get their postback values set before OnLoad is called. If you add your controls after that point, they won't get their values.
BSantiag
Member
8 Points
5 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 04:27 PM|LINK
The controls are being created by a method I created called "PreFormatGrid", which is called in the controls Load() event.
Programmer Analyst
(SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS, Cognos ReportNet)
rossisdead2
Participant
1313 Points
300 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 04:39 PM|LINK
That's the problem. You need to add the controls during OnInit.
BSantiag
Member
8 Points
5 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 05:00 PM|LINK
That's not an option. The Checkboxes need to have unique ID's, and the ID names I create for each, encode a RecordID number inside it (ie:chkSelect_25805). Since Checkboxes don't offer a value submit property, this was the only way I could think of getting what I needed. I parse the name of the Checkbox at the time of PostBack to know which record it's referring to. The issue is, I build the Checkbox name dynamically (by use of a property that defines the PrimaryKey column name of the DataSource being bound to). At the time of the Init() event, the property I need has no value yet (don't know when it gets populated, all I know is by the time the Load() event execute, the Property I need now has a value), causing the creation method to crash.
Programmer Analyst
(SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS, Cognos ReportNet)
rossisdead2
Participant
1313 Points
300 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 05:40 PM|LINK
Would you know what the IDs are supposed to be by the time LoadPostbackData is called? You could override that method and set the values of the checkboxes there. You could set the values anywhere, really, when you know the ID you're looking for. All the postdata is available in Request.Form(or Request.Params), you'd be able to get the values that way, too.
BSantiag
Member
8 Points
5 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 05:48 PM|LINK
I don't know where the LoadPostBackData() event is, or how to "trap & step through" it in order to give you an answer. Is it a page event, or a control event? How do I hook into it? I tried looking for it (must be a hidden event), and I tried Googling...can't really seem to get an answer to where it is...
On a side note, I know I can set the value anywhere...the problem is that MY value (.Checked = FALSE) is overwritten by the value of the checkbox submitted at the time of the PostBack (.Checked = TRUE). I honestly don't care what the values are of the Checkboxes at all, I only care about the Postback event and the Checkbox control's ID. It's value I want to discard completely. Problem is I can't seem to figure out how...
Programmer Analyst
(SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS, Cognos ReportNet)
rossisdead2
Participant
1313 Points
300 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 06:01 PM|LINK
LoadPostBackData's an overridable method on a control, not an event, sorry.
Right, what I meant is that Request.Form has all the raw posted back values. Since your checkboxes are being created during OnLoad, they don't get to have their values set by LoadPostBackData, which occurs before the OnLoad event fires. You *can* however get their value from Request.Form as long as you know their UniqueID. Request.Form["some$control$name"] will either return "true" if the checkbox was checked, or null if it wasn't.
BSantiag
Member
8 Points
5 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 07:09 PM|LINK
Sorry, still don't understand how I can hook into this Method, event or not. I can't find it. Visual Studio just complains that LoadPostBackData() doesn't exist.
Well, only the Checkbox Control that caused the PostBack is listed in the Request.Forms collection, the other controls are not listed. This is part of my confusion and frustration. The values don't even exist in the Request.Forms collection, so how they get to "retain" their value is confusing me. I did read up that even disabling ViewState doesn't help because controls have an additional ControlState that cannot under any circumstances be disabled, and of course this information doesn't show up in the Request.Forms collection. But like I stated, I don't care about the values. I don't want them at all. I want them gone, cleared, deleted, removed. I do not want the Checkbox control to remember it's previous value...I want it to render after PostBack clean, unchecked. I never would have guessed it would be so difficult to do this...
Programmer Analyst
(SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS, Cognos ReportNet)
rossisdead2
Participant
1313 Points
300 Posts
Re: Dynamically Created Checkboxes in GridView Retain Old Values
Feb 29, 2012 09:11 PM|LINK
Could you post a small sample project that reproduces the problem?