问题描述
我们有一个现有的ASP.Net Web应用程序,我们希望使用母版页将其转换为Web应用程序.在执行此过程的过程中,我发现为HTML元素生成的HTML ID以ContentPlaceHolder的ID为前缀.这是我们设置ContentPlaceHolder的clientidmode = static时可以预期的结果.现在,由于我们已有许多使用id的现有客户端脚本,因此当我们使用母版页时,此部分会中断,并且遍历所有JavaScript以确保我们使用Control.ClientID调用javascript是一项艰巨的工作,因为其中很多都是硬编码的.
We have an existing ASP.Net web application which we want to convert into using masterpages.In the process of doing this, I found that the HTML id's generated for the HTML elements are prefixed with the ContentPlaceHolder's id. And this is what can be expected when we set the ContentPlaceHolder's clientidmode=static.Now since we have a lot of existing client side scripts that make use of the id's, this part breaks when we use masterpages, and it is quite a big job to run through all our javascript to make sure we call the javascript using Control.ClientID, as a lot of it is hardcoded.
有没有一种方法可以禁用前缀?如果我创建了每个设置其ClientIdMode = static的控件,那么我可以成功做到这一点,但是我还是希望设置一次,以确保所有控件都具有其ClientIdMode = static.那可能吗?还是可以覆盖ContentPlaceHolder的NamingContainer?
Is there a way to disable the prefixing? I can succeed doing this, if I create every control setting its ClientIdMode=static, but then again I would prefer settings this once, to ensure that all controls are have their ClientIdMode=static. Is that possible? Or is it possible to override the NamingContainer of the ContentPlaceHolder?
平台是.Net 4.0
The platform is .Net 4.0
(如下面的答案所述,在web.config中解决了ClientIdMode = static的上述问题之后),我遇到了一个问题,即名称"属性是自动生成的,并且没有设置为原来的值在介绍母版页之前.这给我现有的服务器代码带来了问题,该服务器代码中有许多Request.Form [].知道这里有什么最佳实践可以解决这个问题吗?
(After fixing the above problem with ClientIdMode=static in the web.config as described in the answer below), I have bumped into the problem that the "name" attribute is automatically generated, and is not set to whatever it was before I introduced masterpages. This gives me a problem with my existing server code, which has many Request.Form[]. Any idea what best practice is here to solve this problem?
谢谢圣战
推荐答案
您可以在页面级使用ClientIDMode
:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ClientIDMode="Static" %>
母版页级别:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" ClientIDMode="Static" %>
和Web.Config级别(使所有页面都继承此行为):
and Web.Config level (making all pages inherit this behavior):
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages clientIDMode="Static"></pages>
</system.web>
这篇关于如何禁用/覆盖内容页面的控件ID的命名容器ID的生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!