简体   繁体   English

asp.net 4.0:INPUTs的名称是否有ClientIDMode的等价物?

[英]asp.net 4.0: is there any equivalent of ClientIDMode for the names of INPUTs?

I have an asp:ListView whose ClientIDMode is set to Predictable. 我有一个asp:ListView其ClientIDMode设置为Predictable。 Its ItemTemplate contains an asp:textbox . 它的ItemTemplate包含一个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: 文本框的ID正如我所期望的那样,但它的name仍然使用看起来像AutoID样式的算法:

<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? 有没有办法让我的输入名称像ID一样?

(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 . 输入元素的Name是POST数据中的名称,因此如果回发改变了ListView绑定到的列表(例如,交换两个元素),则文本框中的值最终会与错误的键关联,因为框架是基于Name而不是ID来关联它们。

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? 如何从服务器控件中删除'name'属性?

I over-rode the RenderChildren method on a Page control as I just wanted full control of the HTML for a few controls: 我在Page控件上过度使用了RenderChildren方法,因为我只想完全控制一些控件的HTML:

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. 如果你尝试这样做,你需要知道你正在做什么,WebForms会认为控件丢失了,所以你不能在任何回发中使用它。 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. 为了我的目的,我想在服务器或客户端添加任意数量的多行,而不必处理.Net Ajax控件,它工作正常。

I'm pretty sure you can't change the name, especially when you modify the ClientIDMode. 我很确定你无法更改名称,尤其是在修改ClientIDMode时。 As an alternative, you can add a Title attribute. 或者,您可以添加Title属性。 VS will flag this as unknown in the server side code, but it renders correctly in the HTML. VS会在服务器端代码中将此标记为未知,但它会在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>

A jquery solution 一个jquery解决方案

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

//Use a HtmlGenericControl //使用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;

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. 该名称对应于UniqueID属性,该属性由系统生成,您无法控制。 Seems you have to find a way to achieve what yo want using only the control ID. 似乎你必须找到一种方法,只使用控件ID来实现你想要的东西。

Both names are using the predictable pattern; 两个名字都使用可预测的模式; originally, name also equaled ct100_ct100 etc. From what I see, that's a predictable name. 最初,名字也等于ct100_ct100等。从我看到的,这是一个可预测的名称。 Client ID value will always use _ between control prefixes and Unique ID (name attrib) will always use $. 客户端ID值将始终在控制前缀之间使用_,唯一ID(名称属性)将始终使用$。 The two will always match, except for a few controls that leverage name for something (radiobuttonlist uses for grouping). 这两个将始终匹配,除了一些利用名称的控件(radiobuttonlist用于分组)。

HTH. HTH。

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. 我有一个完全相同的问题,不得不使用“System.Web.UI.Control”中公开的这些属性之一来获取服务器端的客户端控件名称。

Play around with these properties and construct the "Name" in server side yourself and use Request.Form("NameHere") 使用这些属性并自己在服务器端构建“名称”并使用Request.Form(“NameHere”)

Me.ClientIDSeparator
Me.IdSeparator
Me.UniqueID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM