How to disable/override naming container's ID generation of Content page's control id's

烈酒焚心 提交于 2019-12-05 04:24:53

You can have ClientIDMode at Page Level:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ClientIDMode="Static" %>

MasterPage Level:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" ClientIDMode="Static" %>

and Web.Config level (making all pages inherit this behavior):

<system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <pages clientIDMode="Static"></pages>
</system.web>

In case you don't target .Net framework 4, and the clientIDMode enum is not available, you can simply override the control class. Here is an example with HtmlGenericControl but can be done with any other control:

public class NoNamingContainerControl : HtmlGenericControl
{
    public NoNamingContainerControl(string tag) : base(tag) { }
    public override string ClientID
    {
        get
        {
            return this.ID;
        }
    }
}

This code should work on any .Net framework versions, although in .net 4 you should probably use the clientIDMode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!