Cant get rid off viewstate

M

Mariano Drago

Hi there
Im having problems with some checkbox generated at runtime.
In a form, i draw some html checkboxes, each chekbox represents a kinda
"department" the user is linked to. The problems is that when a postback is
generated and the page reloads, the old state (checked) of the checkboxes
remains although i explicit set it to false.
To ilustrate the problem, simply create a form, drag a panel (or
placeholder, its the same) and a button and put this code in page_load
event:

for(int i = 0; i < 10; i++)

{

HtmlInputCheckBox chk = new HtmlInputCheckBox();

chk.ID = "chk_" + i.ToString();

chk.Name = chk.ID;

chk.Checked = false;

Panel1.Controls.Add(chk);

}

No matter if you set "enabledviewstate" to false in the Panel or the in the
page, you will see that after hiting the button the checks will be as you
left they before.

I check with FindControl inside the controlcollection of the panel and its
empty...

How can i avoid that???? How can i set the checkbox to false in every hit?

Thanks in advance.-
 
G

Guest

Try Explicitly Sett the EnableViewState property to False in your code
chk.EnableViewState = false;

Hope this helps. Posting is provided as is
 
M

Mariano Drago

I also look inside the HttpContext for the cached checkboxes (they must be
somewhere!) and found nothing :(
 
T

Teemu Keiski

Hi,

earlier your code to put them unchecked is otherwise correct, but you need
to do that (putting them unchecked) in PreRender as postback data loading
for CheckBoxes (or dynamical controls generally) is done after Page_Load.
And yes, disabling ViewState won't help because this isn't controlled by
ViewState at all.

I've discussed the same behaviour, though with TextBoxes in my blog post:
http://blogs.aspadvice.com/joteke/archive/2004/03/15/767.aspx
 
M

Mariano Drago

Thanks for the answer Teemu.
I readed the blog... and now i understand the problem.
Knowing that, i simple get rid of the checkbox and use:
Label lb = new Label();

lb.Text = "<input name='chk_"+i.ToString()+"' id='chk_"+i.ToString()+"'
type='checkbox' />";

Panel1.Controls.Add(ln);

It isnt elegant, i know, but it works ;)
Thanks again!
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,133
Latest member
MDACVReview
Top