0

I have this code to add a new TextBox to my UserControl:

/// <summary>
/// Create a new TextBox
/// </summary>
/// <returns>a new TextBox with the right name attribute</returns>
private TextBox createTextBox()
{
    TextBox textbox = new TextBox();        
    textbox.Attributes["name"] = name + "_" + (textBoxList.Count + 1);
    textbox.ID = name + "_" + (textBoxList.Count + 1);
    return textbox;
}

on the output HTML the "name" attribute of this TextBox is changed, and the UserControl name is added...

<input name="multipleTextBox$test_1" type="text" id="multipleTextBox_test_1">

how can i avoid the "multiplartTextBox$" part of the name to be added ? it's important since i need my unique name to be right...

Joe
  • 41,484
  • 20
  • 104
  • 125
Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154

3 Answers3

0

by default you cant. this is how ASP.NET and control name generation works especially if you are adding it in a user control. this is how ASP.NET differentiates with controls and naming conflicts on the client side. This is quite normal. why would it matter to you though you? why do you need access to the element at runtime? if you need access to it, simply user document.getElementById('multipleTextBox$test_xxx') :)

you can set the ClientIDMode to static though however I would advise against this unless you have good reason to do so - this would be the ID, and not the name

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
  • i need it in order to get those receive those elements through the Request.Form collection... – Asaf Nevo Nov 25 '13 at 15:27
  • why don't you iterate through the form collection and see if the current element's name begins with multipleTextbox$? if so.. continue the parsing and iteration.... :) – Ahmed ilyas Nov 25 '13 at 15:28
  • use ClientIdMode only set the ID to static, but name still contains multipleTextBox$ – Asaf Nevo Nov 25 '13 at 15:28
  • thought as much. like I said, this is how ASP.NET generates its ID's for client rendering. - remember, this is for the ID and not name property – Ahmed ilyas Nov 25 '13 at 15:29
  • as i see it, a development enviorment that allow me to add TextBox or other Controls that are sent using form, should allow me to decide which name id right for me... that just wierd – Asaf Nevo Nov 25 '13 at 15:30
  • right, but then there comes a point where you maybe wrong.... this is why Microsoft are experts and knows the common issues people face, sometimes the people don't even see the problem! :). As I said... the clientidmode is only for the ID - not the name property. why are you interested in the name property and not ID?! – Ahmed ilyas Nov 25 '13 at 15:31
0

Set the TextBox.ClientIDMode to Static.

TextBox textbox = new TextBox();        
textbox.ClientIDMode = ClientIDMode.Static;

ClientIDMode in ASP.NET 4.0

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @AsafNevo: You're right, i've overlooked the `Name` part. I don't think that you can easily change that behaviour. Here is a related question: http://stackoverflow.com/questions/4973947/asp-net-4-0-is-there-any-equivalent-of-clientidmode-for-the-names-of-inputs – Tim Schmelter Nov 25 '13 at 15:31
0

you can always access your control by ClientId property to get rendered Control ID and UniqueId to get rendered Control Name.

Nogard
  • 1,779
  • 2
  • 18
  • 21