adding controls on client side

O

Oleg Ogurok

Hi all,

I'd like to create a DataGrid-like control that lets a user add a row with a
javscript (DOM), without a postback. Then later a user can submit the whole
form back to the server.

As far as I know, ASP.NET doesn't support this kind of behavior. I think
during the postback, newly added controls might confuse the parsing engine.
But even if it doesn't, how would I get the values from those new controls?

Thanks,
Oleg.
 
K

Ken Dopierala Jr.

Hi Oleg,

No server based framework supports this kind of behavior. Not PHP, J2EE,
ASP.Net, or anything else. What you are doing, you are doing on the client
and for all you know the client is no longer connected to the internet.

Given that, you are in luck because this is extremely easy and when you
finally do get back to the server ASP.Net makes this super simple to get
your values. It sounds like you already know how to create the controls.
Everytime you create a set of controls increment a counter, name each
control in the set the same as in the set before but concat the new counter
value to it. I use this when I have people enter as many line items as they
need for whatever. Also create a hidden input control that runs at the
server and set this control equal to the counter after each set of controls
is added. Now the user submits back to the server, here is where ASP.Net
picks it up again.

Let's say for each line item you had two text boxes (txtFirstName and
txtLastName). The user entered 3 names so coming back to the server you
have:

txtFirstName1, txtLastName1, txtFirstName2, txtLastName2, txtFirstName3,
txtLastName3

To get your values all you need to do is loop through these like so:

Dim intNumberOfLineItems As Integer
intNumberOfLineItems = CInt(hidCount.Value)

For intCounter = 1 To intNumberOfLineItems
strFirstName = CStr(Request("txtFirstName" + CStr(intCounter)))
strLastName = CStr(Request("txtLastName" + CStr(intCounter)))
'Save to DB or whatever.
Next

That's all there is to it! Good luck! Ken.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top