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

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

问题描述

控件的下列asp.net侧code:

The following asp.net side code of control:

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

生成HTML的等code:

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.

我如何prevent'名'属性的产生?谢谢

How can I prevent 'name' attribute generation? Thanks

P.S。我ASP.NET 4.0下工作。

P.S. I work under ASP.NET 4.0

推荐答案

创建过滤器(类从继承),它分配给你的 HttpContext.Response.Filter 属性,并在其中,你会覆盖的方法,去除生成的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 :)

请参阅此页面了解更多信息<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponse.filter.aspx\">http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponse.filter.aspx

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

更新

查看源$ C ​​$ C为文本框这表明名称实际上是添加到属性 - 列表中呈现,所以它应该有可能与文本框类和prevent被添加这个属性的渲染干涉。这应该做

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

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

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