If you are creating the controls on the client-side, you might not
necessarily want to create the control tree on the server-side. Rather,
to read the values of the dynamically created controls, you can just use
Request.Form["formFieldName"] (or Request.Form("formFieldName") in VB.NET).
There are a couple of hacks you could do to get the number of controls
that were created dynamically on the client-side. You could use a
hidden <input> field whose value is set via JavaScript code. Another
approach is you could intelligently name the dynamically created
controls using some scheme, like "dyn1", "dyn2", etc. When reading in
the controls, once you hit a gap (i.e., there's a dyn1 and dyn2, but no
dyn3), you could stop trying, knowing that there are two dynamic controls.
hth
[quoted text, click to view] CS Wong wrote:
> 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/default.asp?url=/library/en-us/dnaspp/html/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
>
>
--
Scott Mitchell
mitchell@4guysfromrolla.com
http://www.4GuysFromRolla.com