如何使用母版页ASP.Net防止内容占位符来自控件ID [英] How to prevent content placeholder from control id using master page ASP.Net

查看:207
本文介绍了如何使用母版页ASP.Net防止内容占位符来自控件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将.net2与母版页一起使用.如何防止在应用程序运行时更改控件ID(例如,将ID"txtName"更改为"ctl00_ContentPlaceHolder1_txtName").是否可以?

I am using .net2 with master page.how can I prevent control id changing while application running(e.g id ''txtName'' changes to ''ctl00_ContentPlaceHolder1_txtName'').Is this possible?

推荐答案

您可以使用ClientIDMode属性来强制控件的客户端ID.如果不希望将其更改为自动生成的名称,则可以将其设置为ClientIDMode ="Static".
You can use the ClientIDMode attribute to force the client ID of the control. If you don''t want it to be changed to the auto generated name you may set it to ClientIDMode="Static".


设置ClientIDMode [
Set ClientIDMode[^] to Static.
报价:

静态-> ClientID值设置为ID属性的值.如果控件是一个命名容器,则该控件将用作其包含的任何控件的命名容器层次结构的顶部.

Static-> The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.


最后,我已经通过下面的代码.


受保护的重写void Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter HTML = new StringWriter();
HtmlTextWriter渲染=新的HtmlTextWriter(Html);
base.Render(Render);
writer.Write(Html.ToString()
.Replace("name = \" ctl00_ContentPlaceHolder1_," name = \")
.Replace("id = \" ctl00_ContentPlaceHolder1_," id = \")));
}
Finally i have solved this by the below code.


protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter Html = new StringWriter();
HtmlTextWriter Render = new HtmlTextWriter(Html);
base.Render(Render);
writer.Write(Html.ToString()
.Replace("name=\"ctl00_ContentPlaceHolder1_", "name=\"")
.Replace("id=\"ctl00_ContentPlaceHolder1_", "id=\""));
}


这篇关于如何使用母版页ASP.Net防止内容占位符来自控件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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