Visibility on postback of dynamically-created HTML controls

  • Thread starter Thread starter Marcus
  • Start date Start date
M

Marcus

I have a problem maybe one of you could help me with. I've created a data
entry screen with lots of dynamically-created client-side controls. I
create HTML texboxes client-side by assigning a value to the td.innerHTML
property. The UI is done, and I now want to post back the user's changes
and update my business object in .NET. But when I postback, I can't see any
of my dynamically created HTML controls in VB .NET. How do I make them
visible?

I understand that in order to make my HTML controls visible to .NET on
postback, I have to include the runat='server' property. But when I include
this in my dynamically-created control, it works fine as long as there are
no variables in the declaration of the control. For example,


The following works fine -- I can see the value on postback in .NET by
typing request.item("fff"):

var abc = '<INPUT type="text" id="fff" runat='server' value="abc" >';

objTD.innerHTML = abc



And this works too -- No compile error, but I can't see the value on
postback:

var myID = "fff"

var abc = '<INPUT type="text" id="' + myID + '" value="abc" >';

objTD.innerHTML = abc



But I get a parse error telling me that + myID + is not a valid identifier
if I do this.

var myID = "fff"

var abc = '<INPUT type="text" id="' + myID + '" runat='server'
value="abc" >';

objTD.innerHTML = abc



Apparently, if you specify a runat='server' parameter, the .NET compiler
fully evaluates the value assigned to innerHTML at compile-time, rather than
at runtime, but it evaluates it at runtime if the runat parameter is not
there. Unless I want all of my TD declarations to have the same ID, I need
a way to change the ID for each field.



Question 1: Is there a way for me to dynamically create HTML controls that
have unique IDs that are visible to the server?



Question 2: If not, is there a better way to get my data, which currently
lives as a two-dimensional array in Javascript, from the client to the
server?



Sorry for the long post. Thanks for any thoughts or input you might have.



Marcus
 
Dynamically created controls have to be handled differently from declared
controls,. as they are 'lost' during the page_load event after postback. The
normal way to handle them is to recreate them in the page_init and then
repopulate them in the page_load (theres a good article on this @
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx)

Alternatively,as the contents of a form have been posted you can read the
values back out of the request.form's collection.
 

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

Back
Top