如何从服务器控件中删除“名称"属性? [英] how to remove 'name' attribute from server controls?

查看:30
本文介绍了如何从服务器控件中删除“名称"属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的asp.net端控制代码:

The following asp.net side code of control:

<asp:TextBox runat="server" ID="LimitTextBox" Text="20" ClientIDMode="Static" />

生成这样的 HTML 代码:

Generates such HTML-code:

<input name="ctl11$ctl00$ctl02$TeamPlayerSelector$LimitTextBox" 
    type="text" value="20" id="LimitTextBox">

ID 属性 - 这是必需的,但如何删除名称"属性?对我来说不是必需的,传输到用户浏览器也太长了.

ID attribute - as is required, but how can I remove 'name' attribute? It is not required for me and is also too long to transfer it to user browser.

如何防止生成名称"属性?谢谢

How can I prevent 'name' attribute generation? Thanks

附言我在 ASP.NET 4.0 下工作

P.S. I work under ASP.NET 4.0

推荐答案

创建一个过滤器(继承自 Stream 的类),将它分配给你的 HttpContext.Response.Filter 属性,并在其中覆盖 Write 方法,以从生成的 html 中删除所有名称标签 :)

create a Filter (class that inherits from Stream), assign it to your HttpContext.Response.Filter attribute, and in it you would overwrite the Write method, to remove all the name-tags from the generated html :)

有关详细信息,请参阅此页面 http://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx

See this page for more information http://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx

更新

查看TextBox的源代码,发现Name实际上是在渲染过程中添加到Attributes-list中的,所以应该有可能干扰渲染TextBox 类并阻止添加此属性.应该这样做

Looking at the sourcecode for TextBox it reveals that Name is actually added to the Attributes-list during render, so it should be possible to interfere with the rendering of the TextBox class and prevent this attribute from being added. This should do

public class NoNamesTextBox : TextBox
{
    private class NoNamesHtmlTextWriter : HtmlTextWriter
    {
        public NoNamesHtmlTextWriter(TextWriter writer) : base(writer) {}

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return;

            base.WriteAttribute(name, value, fEncode);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        var noNamesWriter = new NoNamesHtmlTextWriter(writer);

        base.Render(noNamesWriter);
    }
}

再次更新

我怎么会忘记!您甚至不需要子类化您的文本框.在 asp.net 中,您可以定义每个控件要使用的 HtmlTextWriter 类型,因此您可以像这样配置每个 TextBox 控件应该使用您自己的 NoNamesHtmlTextWriter 实例

How could i forget! You don't even need to subclass your textbox. In asp.net you can define which HtmlTextWriter type you want to use per control, so you can just configure that every TextBox control should use an instance of your own NoNamesHtmlTextWriter like this

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter 
        controlType="System.Web.UI.WebControls.TextBox"
        adapterType="NoNamesTextBoxAdapter" 
      />
    </controlAdapters>
  </browser>
</browsers>

public class NoNamesTextBoxAdapter : ControlAdapter
{
    private class NoNamesHtmlTextWriter : HtmlTextWriter
    {
        public NoNamesHtmlTextWriter(TextWriter writer) : base(writer) { }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return;

            base.WriteAttribute(name, value, fEncode);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        var noNamesRender = new HtmlTextWriter(writer);
        base.Render(noNamesRender);
    }
}

这篇关于如何从服务器控件中删除“名称"属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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