Dynamic Control Creation and ViewState: what is the ASP.Net way?

Discussion in 'ASP .Net Web Controls' started by PABruceFan, Jul 30, 2004.

  1. PABruceFan

    PABruceFan Guest

    I am dynamically adding rows to an asp:table as a result of a database
    query. The row consists of a label, a textbox and three checkboxes in
    that order:

    LABEL TEXTBOX CHECKBOX1 CHECKBOX3 CHECKBOX2
    name1 email1 checked checked checked
    name2 email2 not checked checked checked

    I have no problem adding the dynamic controls...

    Now, suppose the user clicks on name2/checkbox1 and changes the state
    to checked then hits the submit button...I need to write this change
    to my database. The problem is that since the controls are dynamic,
    there are no page variables that I can access to determine the new
    value. To obtain the value I do the following:

    string state = Request.Form.Get( "Row2_CheckBox1" );

    There is no problem with this and it works fine, it just seems like it
    NOT the ASP.NET way of doing things. Is there a better, more ASP.NET
    way of doing things?????

    TIA-

    Barry
    PABruceFan, Jul 30, 2004
    #1
    1. Advertising

  2. PABruceFan

    Ben Guest

    Why aren't you using a datagrid?

    "PABruceFan" <> wrote in message
    news:...
    > I am dynamically adding rows to an asp:table as a result of a database
    > query. The row consists of a label, a textbox and three checkboxes in
    > that order:
    >
    > LABEL TEXTBOX CHECKBOX1 CHECKBOX3 CHECKBOX2
    > name1 email1 checked checked checked
    > name2 email2 not checked checked checked
    >
    > I have no problem adding the dynamic controls...
    >
    > Now, suppose the user clicks on name2/checkbox1 and changes the state
    > to checked then hits the submit button...I need to write this change
    > to my database. The problem is that since the controls are dynamic,
    > there are no page variables that I can access to determine the new
    > value. To obtain the value I do the following:
    >
    > string state = Request.Form.Get( "Row2_CheckBox1" );
    >
    > There is no problem with this and it works fine, it just seems like it
    > NOT the ASP.NET way of doing things. Is there a better, more ASP.NET
    > way of doing things?????
    >
    > TIA-
    >
    > Barry
    Ben, Jul 30, 2004
    #2
    1. Advertising

  3. DataGrid, DataList, Repeater.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "PABruceFan" <> wrote in message
    news:...
    > I am dynamically adding rows to an asp:table as a result of a database
    > query. The row consists of a label, a textbox and three checkboxes in
    > that order:
    >
    > LABEL TEXTBOX CHECKBOX1 CHECKBOX3 CHECKBOX2
    > name1 email1 checked checked checked
    > name2 email2 not checked checked checked
    >
    > I have no problem adding the dynamic controls...
    >
    > Now, suppose the user clicks on name2/checkbox1 and changes the state
    > to checked then hits the submit button...I need to write this change
    > to my database. The problem is that since the controls are dynamic,
    > there are no page variables that I can access to determine the new
    > value. To obtain the value I do the following:
    >
    > string state = Request.Form.Get( "Row2_CheckBox1" );
    >
    > There is no problem with this and it works fine, it just seems like it
    > NOT the ASP.NET way of doing things. Is there a better, more ASP.NET
    > way of doing things?????
    >
    > TIA-
    >
    > Barry
    Kevin Spencer, Jul 30, 2004
    #3
  4. "PABruceFan" <> wrote in message
    news:...
    > I am dynamically adding rows to an asp:table as a result of a database
    > query. The row consists of a label, a textbox and three checkboxes in
    > that order:
    >
    > LABEL TEXTBOX CHECKBOX1 CHECKBOX3 CHECKBOX2
    > name1 email1 checked checked checked
    > name2 email2 not checked checked checked
    >
    > I have no problem adding the dynamic controls...
    >
    > Now, suppose the user clicks on name2/checkbox1 and changes the state
    > to checked then hits the submit button...I need to write this change
    > to my database. The problem is that since the controls are dynamic,
    > there are no page variables that I can access to determine the new
    > value. To obtain the value I do the following:
    >
    > string state = Request.Form.Get( "Row2_CheckBox1" );
    >
    > There is no problem with this and it works fine, it just seems like it
    > NOT the ASP.NET way of doing things. Is there a better, more ASP.NET
    > way of doing things?????


    You should be using a DataGrid or such. But if you really have to add the
    controls "manually", then you have to add them on every page request, and in
    the exact same order. If you do this, then the controls will load their
    ViewState as well as the values posted from the form. This will make the new
    values available in the Text or Checked properties, and it will allow the
    controls to raise events when these values change.
    --
    John Saunders
    johnwsaundersiii at hotmail
    John Saunders, Jul 31, 2004
    #4
    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. PABruceFan
    Replies:
    3
    Views:
    4,117
    John Saunders
    Jul 31, 2004
  2. Tex
    Replies:
    4
    Views:
    905
    Spam Catcher
    Feb 9, 2006
  3. mausimo
    Replies:
    0
    Views:
    254
    mausimo
    Sep 24, 2009
  4. Josema
    Replies:
    2
    Views:
    336
  5. Josema
    Replies:
    0
    Views:
    197
    Josema
    May 9, 2005
Loading...

Share This Page