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

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

问题描述

我有一个 asp:ListView ,其ClientIDMode设置为Predictable.其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绑定到的列表(例如,交换两个元素),则文本框的值将结束与错误的键相关联,因为框架是根据 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.

推荐答案

您可以使用以下文章中的方法来更改Input的名称,但要稍加修改:

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天全站免登陆