473,545 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Va lue 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 3112
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*********@SP AMnascencetech. PLScom> wrote in message
news:%2******** ********@TK2MSF TNGP14.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.Va lue 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2911
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have a WebForm with some textboxes, dropdownlists, a panel, imagebutton and so on. When I click on the image button (which was created at design time) I...
1
1458
by: Mike | last post by:
I have a placeholder object in which I am dynamically placing controls based on a database query. Of course during postback these controls go away. I have seen other questions posed where someone wants the dynamically created controls to appear on the page. However in my case I don't really need to have the control displayed after postback, but I...
0
1554
by: AJP | last post by:
I have a child window which does the following after a button is pressed: Response.Write("<script>window.opener.__doPostBack('','');</script>"); but I get the following uncaught exception upon doing this: "An error has occurred because a control with auto-generated id '_ctl139' could not be located to raise a postback event. To avoid...
4
3097
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have...
1
3400
by: Marcus | last post by:
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...
2
2444
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the TemplateColumn dynamically. I persist a DataSet in my Session object and I bind the CheckBox to the DataSet. So far so good. The CheckBoxes show up and the...
4
1936
by: sydney.luu | last post by:
Hello All, I am programmatically building a table on my web page with one row and two columns. The first column contains a web server checkbox dynamically created and the second column simply contains text of whatever I put in. This is running fine. If I check the checkbox, how would I check if this checkbox is checked or not during...
3
1545
by: jeroen.bolle | last post by:
I'm designing a form where the user can decide how many options for a poll he wants to add. There's a "+" button to dynamically add textboxes with javascript, without a postback. How do I access these newly generated <input type="text" id="..." /> elements with ASP.NET to save it to a database?
1
1923
by: karthik25 | last post by:
Hi, The following code creates a tab container, 5 tab panels, buttons, labels. I was able to add event handlers to the button. But how am i supposed to access the dynamically created labels? For example how do i get the text of the label changed when the corresponding button is clicked? I have given the complete code below. Please help me with...
0
7457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7746
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5962
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.