您的位置:首页 > 其它

clientid:怎么获取控件id的名字,用ClientID这样得到id名字

2008-12-25 16:13 323 查看
<script runat="server">
private string _classcolor1 = "id1";
private string _classcolor2 = "id2";
private string _classcolor3 = "id3";
private string _classcolor4 = "id4";
private string _classcolor5 = "id5";
private string _classcolor6 = "id6";
private string _classcolor7 = "id7";
public string classcolor1
{
get
{
return this._classcolor1;
}
set
{
this._classcolor1 = value;
}
}
public string classcolor2
{
get
{
return this._classcolor2;
}
set
{
this._classcolor2 = value;
}
}
public string classcolor3
{
get
{
return this._classcolor3;
}
set
{
this._classcolor3 = value;
}
}
public string classcolor4
{
get
{
return this._classcolor4;
}
set
{
this._classcolor4 = value;
}

}
public string classcolor5
{
get
{
return this._classcolor5;
}
set
{
this._classcolor5 = value;
}
}
public string classcolor6
{
get
{
return this._classcolor6;
}
set
{
this._classcolor6 = value;
}
}
public string classcolor7
{
get
{
return this._classcolor7;
}
set
{
this._classcolor7 = value;
}
}
</script>

<body>
<div class="Navmenu">
<ul>
<li id="<%=classcolor1 %>"><a href="/index.aspx" target="_parent"><span>
<asp:Literal ID="Label1" runat="server" EnableViewState="false" Text="首 页"></asp:Literal></span></a></li>
<li id="<%=classcolor2 %>"><a href="/Info/CompanyIntroduce.aspx" target="_parent"><span>
<asp:Literal ID="Label2" runat="server" EnableViewState="false" Text="企业介绍"></asp:Literal></span></a></li>
<li id="<%=classcolor3 %>"><a href="/Info/NewsCenter.aspx" target="_parent"><span>
<asp:Literal ID="Label3" runat="server" EnableViewState="false" Text="新闻中心"></asp:Literal></span></a></li>
<li id="<%=classcolor4 %>"><a href="/Product/Default.aspx" target="_parent"><span>
<asp:Literal ID="Label4" runat="server" EnableViewState="false" Text="商品展示"></asp:Literal></span></a></li>
<li id="<%=classcolor5 %>"><a href="/Down/DownCenter.aspx" target="_parent"><span>
<asp:Literal ID="Label5" runat="server" EnableViewState="false" Text="下载中心"></asp:Literal></span></a></li>
<li id="<%=classcolor6 %>"><a href="/Blog/Sitepage/sitespace.aspx" target="_parent">
<span>
<asp:Literal ID="Label6" runat="server" EnableViewState="false" Text="博客"></asp:Literal></span></a></li>
<li id="<%=classcolor7 %>"><a href="/BBS/cebbs/" target="_parent"><span>
<asp:Literal ID="Label7" runat="server" EnableViewState="false" Text="论坛"></asp:Literal></span></a></li></ul>
</div>
</body>

为什么id的名字要用属性的写法是因为在程序编译后id的名字会变,用属性命名字就不会发生变化,前面不会多出其他的字符串。
我之前也曾经写过id名字会变的原因是因为system.web.ui.usecontrol的原因。加了模板会出现这样的问题
要想得到变化了的控件id的名字,可以<%txtName.ClientID%>得到控件id ,还有一种就是我上面写的代码就是用属性给控件命名就可以了。但是属性命名有点麻烦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: