我可以强迫asp将名称设置为与id相同 [英] Can I force asp to set name the same as id

查看:94
本文介绍了我可以强迫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中包含namingcontainer,因此效果很好.我相信页面上永远不会有两个相同的控件,所以这可以正常工作.

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属性的呈现,例如此问题的以下示例:

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相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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