Bad ViewState values for dynamically added checkbox

Discussion in 'ASP .Net' started by eSapient, Apr 24, 2004.

  1. eSapient

    eSapient Guest

    I created a web page which contains a form which contains a table. The
    number of rows for the table are determined dynamically. The first cell of
    each row contains a HyperLink control and the last cell contains a CheckBox
    control. In order for the ViewState mechanism to work correctly, I:

    1. create the structure (add rows/columns) of the table in the OnInit
    handler when it is a PostBack,
    2. create the table structure and populate its cells in the page's Load
    handler when it is not a PostBack.

    The page works correctly most of the time.

    I have two questions:

    1. Inside OnInit on a PostBack, where I create the table structure, I need
    to know the number of rows to be added to the table. However, per my
    understanding, in OnInit, the ViewState has not been parsed, so I cannot use
    a ViewState value like ViewState["RowCount"]. So I am forced to use
    Session["RowCount"], but don't like it. Is there a better way? I am
    presuming using a HtmlInputHidden control would suffer from the same problem
    of unavailable ViewState values. I prefer not to use cookies.

    2. Sometimes, the ViewState values are not correct. I have a button in the
    form which deletes rows from the table. In my test, I have three rows. I
    click on the button to remove one row and that is successful. I repeat again
    successfully with another row. When there is only one row left in the table
    and I select its CheckBox, on PostBack, the CheckBox shows as unchecked. As
    a result, the row does not get deleted. Sometimes, not just when there is
    one row, I see bad CheckBox states on postback, though it occurs less often.
    Any ideas about the cause of this problem?

    Thanks in advance.
    eSapient, Apr 24, 2004
    1. Advertisements

  2. Hi

    Seems u got a good hold on ViewState ..

    Just try out creating yo 'Dynamic' table in 'Page_Load' event ..

    Tell me if it doesn't work()
    =?Utf-8?B?SG1udA==?=, Apr 25, 2004
    1. Advertisements

  3. eSapient

    eSapient Guest


    Thank you very much for your help.

    You asked me to try to create the dynamic table in the
    page load. However, that is what I tried right at the
    beginning and ran into problems because ViewState had
    already been parsed before Page_Load, so creating
    controls inside Page_Load did not help in populating the
    data in the dynamically added rows. I think I may have
    not understand you.

    What I tried now was to recreate the tables (both, the
    structure and the dynamic data contents) at the end of
    the handlers for the Click events of the buttons which
    modify the table data (including one button which deletes
    rows). I thought this would be the right thing to do
    since I would be sending back a refreshed snapshot of the
    database. However, I continue to see bad values of the
    checkboxes' Checked property values on PostBack.

    Here are links to the code of the page I created:

    Thanks in advance again.
    eSapient, Apr 25, 2004
  4. eSapient

    eSapient Guest


    I saw that in another thread ("Kill ViewState !!!") you
    had mentioned that Request.Form could be use to retrieve
    control values on a PostBack. I checked and found that
    that is true. I plan to use that instead of relying on
    ViewState. Thanks.
    eSapient, Apr 25, 2004
    1. Advertisements

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. =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=

    Confusion Over Postback, ViewState and Dynamically Added Controls

    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=, May 3, 2005, in forum: ASP .Net
    Brock Allen
    May 3, 2005
  2. silverfox_1188
    Jul 25, 2006
  3. =?Utf-8?B?QmlsbDQ0MDc3?=

    Dynamically added Checkbox in Gridview

    =?Utf-8?B?QmlsbDQ0MDc3?=, Aug 1, 2006, in forum: ASP .Net
  4. Replies:
    Kevin Spencer
    Apr 25, 2007
  5. rantingrick
    Peter Pearson
    Jul 13, 2010