0

Do any one know how can I get the textbox value based on the name in controller?

Example:

<asp:TextBox runat="server" ID="stDate" Text='<%# Eval("StartDate")%>' Width="6em" />

When I read from the source code, it's show as below:

<input name="ctl00$cplh$GridView1$ctl10$stDate" type="text" id="stDate" style="width:6em;" />

How can I get the get this textbox value based on the name ctl00$cplh$GridView1$ctl10$stDate in the controller?

NOTE: the reason I would like to do in this way is because I have more then 1 textbox are using the same ID (stDate)

John Arlen
  • 6,129
  • 2
  • 30
  • 37
Jin Yong
  • 38,582
  • 71
  • 132
  • 177
  • why not write the name property directly instead of looking at generated one – COLD TOLD May 15 '12 at 01:31
  • RE `NOTE`: is this even possible? I was under the impression that each element, whether regular HTML or ASP.NET, had to have a unique `ID`. Kind of the purpose of calling it an _ID_, it _identifies_ the element. – kaveman May 15 '12 at 01:32
  • It's possible but doesn't follow conventions of XHTML, and goes against best practices. all and all it shouldn't be done. – Blast_dan May 15 '12 at 01:33

3 Answers3

0

To get the ID generated on the page, you have to get the ClientID

stDate.ClientID
Phil-R
  • 2,103
  • 16
  • 20
0

there's a few things I want to suggest:

  • ID's must be unique, you should assign an unique identifier to each controller in your page.
  • .NET needs a hook to your control - so it needs to set the name if it is going to be created by .NET (you can't change it even if you try to force it).
  • If you want to assign a common name between controllers use the attribute class, for example: <input id="txtbox1" class="commonController" type="text" value="input1"> <input id="txtbox2" class="commonController borderClass" type="text" value="input2">

p.d.: remember, you can assign multiple classes to a controller.

  • Also remember that id is only for client side DOM manipulation... name is required for the browser to actually post the content

If you want more info check here and here

Community
  • 1
  • 1
Luis
  • 5,400
  • 8
  • 38
  • 60
0

If you using ASP.NET 4 you can you set the ClientIDMode to static on the textbox then asp keeps the id as you had set it.

<asp:TextBox runat="server" ID="stdate" ClientIDMode="Static" />

This helps if you need to use the textbox in client side scripting. Here's an article on it. You would need to make sure that the id is unique then, if you need a common name it would be better to use a class as Luis suggested.

Jonathan
  • 593
  • 6
  • 16