asp.net 4.0:对于 INPUT 的名称是否有任何等效的 ClientIDMode? [英] asp.net 4.0: is there any equivalent of ClientIDMode for the names of INPUTs?

查看:14
本文介绍了asp.net 4.0:对于 INPUT 的名称是否有任何等效的 ClientIDMode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 asp:ListView,其 ClientIDMode 设置为可预测.它的 ItemTemplate 包含一个 asp:textbox.

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

文本框的 ID 与我预期的一样,但它的 name 仍在使用看起来像 AutoID 风格的算法:

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

有没有办法让输入的名称像 ID 一样?

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

(针对以下问题进行)

输入元素的 Name 是 POST 数据中的内容,因此如果回发更改了 ListView 绑定到的列表(例如,交换两个元素),则来自文本框的值将结束up 与错误的键相关联,因为框架根据 Name 而不是 ID 将它们关联起来.

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.

推荐答案

您可以使用以下帖子中的方法更改输入的名称,但稍作修改:

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

如何从服务器中删除名称"属性控制?

我在 Page 控件上使用了 RenderChildren 方法,因为我只想完全控制几个控件的 HTML:

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);
    }
}

如果您尝试这样做,您确实需要知道自己在做什么,WebForms 会认为控件丢失,因此您无法在任何回发中使用它.出于我的目的,我想在服务器端或客户端添加任意数量的多行而无需处理 .Net Ajax 控件,它工作正常.

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.

这篇关于asp.net 4.0:对于 INPUT 的名称是否有任何等效的 ClientIDMode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆