I have a whole bunch of Image Controls, Labels, Table Controls, TableRows, TableCells, etc... that get dynamically created based on how many records are pulled out of the database. Once they load and I hit a button on that page, ONLY the dynamically created
controls lose all of their state. They disappear actually. So, what am I doing wrong here? ViewState cure this problem?
I'm having pretty much the same problem. I checked the link, but am still missing something. I have a custom "Voter" control which I need on the page in 2 locations (for a total of 11 control instances). The first is at the top of the page. This allows the
user to rate the page they are viewing. The second location (contains 10) is in a datagrid which displays the top 10 results from a SQL Query. These voter controls then allow the user to vote on the datagrid items. So the page would look like:
Rate this Page [voter control 1]
Item Name Vote
Item 1 [voter control 2]
...
Item 10 [voter control 11]
I add [voter control 1] to the page dynamically in the template because not every page uses the rating option.
My .aspx page looks like so:
As you can see I set enableViewState="true" for the table, the row, and the cell.
Then in the code behind I have the following code making up the page_load:
If Not Page.IsPostBack Then
Dim voterTest As Control = LoadControl("Voter.ascx")
With CType(voterTest, Voter)
.MinVote = 12
.MaxVote = 18
.VotingFor = "Page"
End With
voterTest.EnableViewState = True
voterTest.ID = "PageVoter"
TestCell.Controls.Add(voterTest)
BindTestDG()
End If
The Voter
control is fairly simple. If the user has not yet voted then it displays a dropdown list with options ("-Vote-", all integers from MinVote to MaxVote) and a button. If the user has voted, or after the user clicks on the button it then changes to a label with
text displaying the results. When ANY event fires on the page the voter control to rate the page disappears. All other controls are just fine. They maintain their exact state. Even if I click the Vote button in the Page Ratings voter control it still disappears.
If I move the code to load the control out of the "if not Page.IsPostBack" check and perform the loading every time page_load is called, it works just fine. The thing is, I don't want to load that control every time. Isn't it a waste to reload a control that
hasn't changed? I mean, it's "bad" to rebind a datagrid every time the page posts back, so isn't it bad to reload a user control? Thanks, riddelrp
Hi, You need to load the dynamical control on every request (that happens outside the IsPostBack check on every time), that just is how it goes and there's no way around it as long as you load the control from the file. Basically it is "bad", but of course
depends also on the counts how many controls you load.
How do I load the control if not from the file if it is a user control? The reason I ask is because I will have another page coming soon which will have 17 fairly large controls on the page, all needed to be loaded dynamically because the users will have the
option of only displaying certain ones. Thanks, -riddelrp
If you have it as code-behind, you can always create the instance of the code-behind class (which derives from user control), but in normal scenario where you have layout details(=controls) in ascx and code in ascx.vb or ascx.cs, you end up with problems as
code-behind doesn't actually know about existence of it's child controls(which are in the ascx file) unless you provide the creation of child controls into the class file as well. That again goes to same problem domain as is with normal custom web controls
and I in fact recommend that in that case, you'd develop such instead of user control.
mattb
Contributor
2333 Points
533 Posts
ViewState with dynamically created Controls
Sep 16, 2003 04:16 AM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: ViewState with dynamically created Controls
Sep 16, 2003 08:00 AM|LINK
Teemu Keiski
Finland, EU
mattb
Contributor
2333 Points
533 Posts
Re: ViewState with dynamically created Controls
Sep 16, 2003 12:13 PM|LINK
riddelrp
Participant
874 Points
175 Posts
Re: ViewState with dynamically created Controls
Sep 16, 2003 07:15 PM|LINK
If Not Page.IsPostBack Then Dim voterTest As Control = LoadControl("Voter.ascx") With CType(voterTest, Voter) .MinVote = 12 .MaxVote = 18 .VotingFor = "Page" End With voterTest.EnableViewState = True voterTest.ID = "PageVoter" TestCell.Controls.Add(voterTest) BindTestDG() End IfThe Voter control is fairly simple. If the user has not yet voted then it displays a dropdown list with options ("-Vote-", all integers from MinVote to MaxVote) and a button. If the user has voted, or after the user clicks on the button it then changes to a label with text displaying the results. When ANY event fires on the page the voter control to rate the page disappears. All other controls are just fine. They maintain their exact state. Even if I click the Vote button in the Page Ratings voter control it still disappears. If I move the code to load the control out of the "if not Page.IsPostBack" check and perform the loading every time page_load is called, it works just fine. The thing is, I don't want to load that control every time. Isn't it a waste to reload a control that hasn't changed? I mean, it's "bad" to rebind a datagrid every time the page posts back, so isn't it bad to reload a user control? Thanks, riddelrpjoteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: ViewState with dynamically created Controls
Sep 16, 2003 07:30 PM|LINK
Teemu Keiski
Finland, EU
riddelrp
Participant
874 Points
175 Posts
Re: ViewState with dynamically created Controls
Sep 16, 2003 07:37 PM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: ViewState with dynamically created Controls
Sep 17, 2003 06:00 AM|LINK
Teemu Keiski
Finland, EU