I have a asp.net usercontrol (.ascx). the user control has a HTML hidden field.
<input id="HiddenField1" type="hidden" runat="server" />
the user control fires a HTML window pop up, users enter several values in the pop up, then after the pop up is closed a string value is saved in that hidden field.
document.getElementById("HiddenField1").value = windowResult;
finally the back end code of the user control does some tasks by picking up the string value from the hidden field.
string[] nameValuePair = new string[2];
nameValuePair = HiddenField1.Value.Split('%');
The problem is the document.getElementById(“HiddenField1”) is failing while execution. because when I use the user control in a web page, the ID of the control becomes [my usercontrol name] + $ + [hidden field name]. (for ex. ctrl_TaxonomyTree1$HiddenField1)
Since the user control will be populated dynamically, I can not always know the name of the instance.
Is there any way to sort this out?
EDIT
The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>)
Error is occuring because of this
When you add the runat=”server” attribute to the tag, the runtime treats it as an HtmlHead control.
EDIT
Solution for the error which you provided in comment
Removed JavaScript from the head section of page and added it to the body of the page and got it working
Check : Article for Error
it will be
because in hidden filed is on server side you neeed to get client id of the filed to get the value in your javascript..
For Asp.net 4.0
If you are using Asp.net 4.0 version than have look to this : ASP.NET 4.0 Client ID Feature
Control.ClientIDMode Property -Gets or sets the algorithm that is used to generate the value of the ClientID property.
AutoID
The ClientID value is generated by concatenating the ID values of each parent naming container with the ID value of the control. In data-binding scenarios where multiple instances of a control are rendered, an incrementing value is inserted in front of the control’s ID value. Each segment is separated by an underscore character (_). This algorithm was used in versions of ASP.NET earlier than ASP.NET 4.
Static
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
Predictable
This algorithm is used for controls that are in data-bound controls. The ClientID value is generated by concatenating the ClientID value of the parent naming container with the ID value of the control. If the control is a data-bound control that generates multiple rows, the value of the data field specified in the ClientIDRowSuffix property is added at the end. For the GridView control, multiple data fields can be specified. If the ClientIDRowSuffix property is blank, a sequential number is added at the end instead of a data-field value. This number begins at zero and is incremented by 1 for each row. Each segment is separated by an underscore character (_).
Inherit
The control inherits the ClientIDMode setting of its NamingContainer control.