asp.net 4.0: is there any equivalent of ClientIDMo

2019-04-18 15:34发布

问题:

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.

回答1:

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.



回答2:

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>


回答3:

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.



回答4:

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.



回答5:

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


回答6:

A jquery solution

 function removeNameAttribute() {
            $('input, select').each(function () {
                $(this).removeAttr("name");
            });
        }


回答7:

//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;