Cant get rid off viewstate

Discussion in 'ASP .Net' started by Mariano Drago, Apr 8, 2004.

  1. 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.-
     
    Mariano Drago, Apr 8, 2004
    #1
    1. Advertising

  2. Mariano Drago

    Guest Guest

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

    Hope this helps. Posting is provided as is



    "Mariano Drago" <> wrote in message
    news:...
    > 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.-
    >
    >
     
    Guest, Apr 9, 2004
    #2
    1. Advertising

  3. Nop, it dosent work.
     
    Mariano Drago, Apr 9, 2004
    #3
  4. I also look inside the HttpContext for the cached checkboxes (they must be
    somewhere!) and found nothing :(
     
    Mariano Drago, Apr 9, 2004
    #4
  5. Mariano Drago

    Teemu Keiski Guest

    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

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke




    "Mariano Drago" <> wrote in message
    news:...
    > I also look inside the HttpContext for the cached checkboxes (they must be
    > somewhere!) and found nothing :(
    >
    >
     
    Teemu Keiski, Apr 9, 2004
    #5
  6. 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!

    "Teemu Keiski" <> escribió en el mensaje
    news:...
    > 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
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    > http://blogs.aspadvice.com/joteke
    >
    >
    >
    >
    > "Mariano Drago" <> wrote in message
    > news:...
    > > I also look inside the HttpContext for the cached checkboxes (they must

    be
    > > somewhere!) and found nothing :(
    > >
    > >

    >
    >
     
    Mariano Drago, Apr 9, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JZ
    Replies:
    3
    Views:
    7,179
    Peter Hansen
    Jul 14, 2004
  2. Nagaraj
    Replies:
    1
    Views:
    912
    Lionel B
    Mar 1, 2007
  3. robert112
    Replies:
    1
    Views:
    474
    Juan T. Llibre
    Apr 26, 2007
  4. Jack Jarmush
    Replies:
    3
    Views:
    4,664
    Jack Jarmush
    Aug 3, 2009
  5. shruds
    Replies:
    1
    Views:
    126
    Java script Dude
    Jan 20, 2006
Loading...

Share This Page