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

P

PABruceFan

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
 
K

Kevin Spencer

DataGrid, DataList, Repeater.

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

John Saunders

PABruceFan said:
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.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top