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