我可以强制asp将名称设置为与id相同吗

我可以强制asp将名称设置为与id相同吗

本文介绍了我可以强制asp将名称设置为与id相同吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理一个包含各种不同搜索控件的表单,但是这些搜索控件现在位于母版页内,因此 ID 被添加到了额外的垃圾中 ('ct100$Body$TextBox_Postal' 而不是 'TextBox_Postal').

I need to process a form full of all sorts of different search controls, however these search controls are now inside a master page and so the id's were getting extra junk added in ('ct100$Body$TextBox_Postal' as opposed to 'TextBox_Postal').

我可以通过设置 ClientIDMode=CliendIDMode.Static 来解决这个问题,这很有效,因为它不会尝试在 ID 中包含命名容器.我相信页面上永远不会有两个相同的控件,所以这会起作用.

I was able to fix this by setting ClientIDMode=CliendIDMode.Static, this works great as it doesn't try and include the namingcontainer in the id. I am confident that there will never be two of the same control on the page so this would work.

问题是,当表单回发时,控件是按名称处理的.名称仍然是 'ct1200$Body$..' 格式,因此 processform 函数无法找到任何控件.有没有办法让 ASP 也在静态"模式下设置名称?

The problem is, when the form is posted back the controls are processed by names. The names are still of the 'ct1200$Body$..' format, so the processform function is unable to find any controls. Is there a way to get ASP to set the names in "Static" mode as well?

推荐答案

简短的回答是否定的,您将不得不覆盖 name 属性的呈现,以下示例来自此问题:ASP.NET:如何从服务器控件中删除名称"属性?

Short answer is no, you will have to override the rendering of the name attribute, example below from this question: ASP.NET: how to remove 'name' attribute from server controls?

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将名称设置为与id相同吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:30