Reputation: 43778
I'm very new in .net c#, just a question here to about the following code. I have wrote a code to display a text box on the screen as below:
<asp:TextBox ID="sUnit" runat="server" MaxLength="12" Width="3em" />
But somehow I failed to using jqueryto assign a value into this textbox:
$('#sUnit').val('test');
Surprisingly when I view the source code, and it show me the ID for the text box is like below:
<input name="ctl00$cplh$sUnit" type="text" maxlength="12" id="ctl00_cplh_sUnit" style="width:3em;" />
Does anyone know how can I get the textbox ID so that I can assigned the value into the textbox?
Hope my question is not sound stupid.
Upvotes: 1
Views: 177
Reputation: 45083
This is due to the fact that UI elements containing these controls are being rendered by ASP.NET, those containers that implement INamingContainer generate the prefix that should be prefixed as per their scope.
From the MSDN line mentioned above:
Any control that implements this interface creates a new namespace in which all child control ID attributes are guaranteed to be unique within an entire application. The marker provided by this interface allows unique naming of the dynamically generated server control instances within the Web server controls that support data binding.
One suggestion in order to find the control itself might be to use the first selector, where you controls are contained within a regular div
. An example to find elements of type input
might look something like:
$('#MyDiv').first('input[type="text"]')
Upvotes: 0
Reputation: 190945
If you are using ASP.NET 4.0, set the ClientIDMode
to static
.
For older versions, use
$('#<%= sUnit.ClientID %>')
Upvotes: 4