17

I have an asp:ListView whose ClientIDMode is set to Predictable. Its ItemTemplate contains an asp:textbox.

The ID of the textbox is acting as I expect it to, but its name is still using what looks like an AutoID-style algorithm:

<input name="lvFields$ctrl0$tbVal" id="lvFields_tbVal_somekey" type="text"/>

Is there a way for me to cause the name of the input to act like the ID does?

(Edit in response to questions below:)

The Name of the input element is what's in the POST data, so if a postback alters the list to which the ListView is bound (for example, exchanging two elements) the values from the textboxes end up associated with the wrong keys, because the framework is correlating them based on the Name and not the ID.

Dan Davies Brackett
  • 9,811
  • 2
  • 32
  • 54
  • Why do you need the name to be `Predictable`? – Daniel A. White Feb 11 '11 at 20:58
  • 1
    If you already have `ID`s that are `Predictable`, why do you need the `Name` to be too? – Oded Feb 11 '11 at 21:01
  • 1
    One reason could be if you wanted to return the selected value of a set of radio buttons in JavaScript? If you have a front end JS/HTML designer and a back end programmer, as it stands the front end programmer would have to understand that referring to the names of any DOM elements was off limits, because those names besides just being ugly could change unexpectedly. – Chris Lukic Dec 22 '11 at 17:26

7 Answers7

2

You can change the name of an Input by using the method from the following post but modifying it slightly:

how to remove 'name' attribute from server controls?

I over-rode the RenderChildren method on a Page control as I just wanted full control of the HTML for a few controls:

protected override void RenderChildren(HtmlTextWriter writer)
{
    var unScrewNamesRender = new RenderBasicNameHtmlTextWriter(writer);
    base.RenderChildren(unScrewNamesRender);
}

private class RenderBasicNameHtmlTextWriter : HtmlTextWriter
{
    public RenderBasicNameHtmlTextWriter(TextWriter writer) : base(writer) { }

    public override void AddAttribute(HtmlTextWriterAttribute key, string value)
    {
        if (key == HtmlTextWriterAttribute.Name && value.Contains("POLine"))
        {
            value = value.Substring(value.LastIndexOf("$") + 1);
        }

        base.AddAttribute(key, value);
    }
}

You do need to know what you're doing if you attempt this, WebForms will think the control is missing so you can't use it in any postbacks. For my purposes, where I wanted to add an arbitrary number of multiple lines either server or client-side without having to deal with .Net Ajax controls, it works fine.

Community
  • 1
  • 1
mattmanser
  • 5,719
  • 3
  • 38
  • 50
1

I'm pretty sure you can't change the name, especially when you modify the ClientIDMode. As an alternative, you can add a Title attribute. VS will flag this as unknown in the server side code, but it renders correctly in the HTML. If you're doing some client-side manipulation, you can address the input as such:

<script type="text/javascript">
$(document).ready(function () {
$('input:text[title="TextBoxName"]').datepicker();
});
</script>
0

A jquery solution

 function removeNameAttribute() {
            $('input, select').each(function () {
                $(this).removeAttr("name");
            });
        }
Siz S
  • 846
  • 3
  • 8
  • 27
0

//Use a HtmlGenericControl

HtmlGenericControl input = new HtmlGenericControl("input");``
input.ID = "lvFields_tbVal_somekey";
input.Attributes.Add("name", "tbVal");
input.Attributes.Add("type", "text");
input.ClientIDMode = ClientIDMode.Static;
0

As far as I know, there is no way to change the name of the input element. The name corresponds to the UniqueID property, which is generated by the system, and which you have no control over. Seems you have to find a way to achieve what yo want using only the control ID.

matk
  • 1,528
  • 2
  • 14
  • 25
0

Both names are using the predictable pattern; originally, name also equaled ct100_ct100 etc. From what I see, that's a predictable name. Client ID value will always use _ between control prefixes and Unique ID (name attrib) will always use $. The two will always match, except for a few controls that leverage name for something (radiobuttonlist uses for grouping).

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

I had the exact same problem once and had to use one of these properties exposed in "System.Web.UI.Control" to get clientside control name in server side.

Play around with these properties and construct the "Name" in server side yourself and use Request.Form("NameHere")

Me.ClientIDSeparator
Me.IdSeparator
Me.UniqueID
Guru Kara
  • 6,272
  • 3
  • 39
  • 50