Dynamic LinkButton, ViewState

A

Andrew Robinson

I have a <asp:table> control with a large number of dynamically created
LinkButtons. I am using the command event, command name and command argument
values in my LinkButtons to trigger actions after the post back. The table
is dynamically generated based on a key field that is stored in ViewState.
This key field is updated by several other controls on the same page.

I am struggling with the pattern of when to create the table. I can't load
it in Page_Init as ViewState is not restored. If I load it at Page_Load
time, I seem to be getting the command name/arguments from the previous
postback and at times, my dynamic LinkButton fails to fire the command
event.

Any suggestions would be appreciated. Thanks,

Andrew
 
T

Teemu Keiski

Hi,

generate it so that you create controls in page_load, but set any proeprties
after you have added the control to Controls collection so that ViewState
tracking has started. And such, state dependant properties need to be set
only once inside If Not IsPostBack check (!IsPostBack in C#), e.g
initially..

here are a some references

Understanding ASP.NET ViewState
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp
Dynamic Web Controls, Postbacks and ViewState
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
Dynamic Controls in ASP.NET
http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
Working with Dynamically Created Controls
http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top