472,143 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Accessing HTMLControls created dynamically using Javascript during PostBack

Hi,

I have a page form where form elements are created dynamically using
Javascript instead of programatically at the code-behind level. I have
problems accessing the dynamically-created elements and would like to seek a
solution for this.

I had looked through several articles for accessing programatically-created
dynamic elements such as:
1)
http://msdn.microsoft.com/library/de.../viewstate.asp
2) http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
3) http://www.codeproject.com/aspnet/retainingstate.asp

I had previously created a page according to those examples and I managed to
detect those elements using the following logic:
1) On first load, I would set a NoOfControls variable which tracks the
number of controls I created programatically.
2) During each PostBack, at the Page_Load stage I would re-create the
controls according to the NoOfControls variable.

The problem I face when I create elements purely at the client-side is that
I can't set the variable tracker similarly at the code-behind. So instead, I
put the tracker as a hidden input and attempt to accessing it during
PostBack. The logic works like this:

1) User would click an "Add Text Field" button and Javascript would create
the text field at the client side. The hidden input NoOfControls would also
increase.
2) When the form is submitted, I would try to access NoOfControls.Value at
Page_Load stage and then allocate the relevant IDs to the created elements.

However, I found that I can't access NoOfControls or any other form inputs
at Page_Load stage as this would give a "Object reference not set to an
instance of an object" error. This denies me the ability to get the values
of the programatically created elements.

Can anybody help me here? To further clarify, I am not trying to re-create
the form in the last view state to show the user. Instead, I am purely
trying to access the form inputs submitted so that I can process them. The
form itself can be shown in its default state.

Thanks!
Wong
Nov 18 '05 #1
1 3005
What you are trying to do isn't possible. When you post back, you post the
form to the page, so only the information in the form goes to the server.
If you create elements on the client side, they will show up visibly, but
that information will never make it to the server.

Best regards,
Jeffrey Palermo

"CS Wong" <pu*********@SPAMnascencetech.PLScom> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

I have a page form where form elements are created dynamically using
Javascript instead of programatically at the code-behind level. I have
problems accessing the dynamically-created elements and would like to seek a solution for this.

I had looked through several articles for accessing programatically-created dynamic elements such as:
1)
http://msdn.microsoft.com/library/de.../viewstate.asp 2) http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
3) http://www.codeproject.com/aspnet/retainingstate.asp

I had previously created a page according to those examples and I managed to detect those elements using the following logic:
1) On first load, I would set a NoOfControls variable which tracks the
number of controls I created programatically.
2) During each PostBack, at the Page_Load stage I would re-create the
controls according to the NoOfControls variable.

The problem I face when I create elements purely at the client-side is that I can't set the variable tracker similarly at the code-behind. So instead, I put the tracker as a hidden input and attempt to accessing it during
PostBack. The logic works like this:

1) User would click an "Add Text Field" button and Javascript would create
the text field at the client side. The hidden input NoOfControls would also increase.
2) When the form is submitted, I would try to access NoOfControls.Value at
Page_Load stage and then allocate the relevant IDs to the created elements.
However, I found that I can't access NoOfControls or any other form inputs
at Page_Load stage as this would give a "Object reference not set to an
instance of an object" error. This denies me the ability to get the values
of the programatically created elements.

Can anybody help me here? To further clarify, I am not trying to re-create
the form in the last view state to show the user. Instead, I am purely
trying to access the form inputs submitted so that I can process them. The
form itself can be shown in its default state.

Thanks!
Wong

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Harry | last post: by
3 posts views Thread by jeroen.bolle | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.