By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Accessing HTMLControls created dynamically using Javascript during PostBack

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.