468,545 Members | 1,760 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

Visibility on postback of dynamically-created HTML controls

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


Nov 19 '05 #1
1 3141
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.

"Marcus" <ma***********@hotmail.com> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
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


Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by moondaddy | last post: by
3 posts views Thread by Guadala Harry | last post: by
2 posts views Thread by celoftis | last post: by
reply views Thread by btysgtmajor | last post: by
1 post views Thread by dontspammenow | last post: by
reply views Thread by rvdnieuwenhuizen | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.