欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 服務(wù)器控件的 ID,ClientID,UniqueID 的區(qū)別

 更新時(shí)間:2010年04月25日 21:52:28   作者:  
asp.net 服務(wù)器控件的 ID,ClientID,UniqueID 的區(qū)別分析,需要的朋友可以參考下。
1、簡(jiǎn)述
ID是設(shè)計(jì)的時(shí)候自己所指定的ID,是我們分配給服務(wù)器控件的編程標(biāo)識(shí)符,我們常常使用this.controlid來尋找控件,那么這個(gè)controlid就是這里所說的ID.
ClientID是由ASP.Net生成的服務(wù)器控件得客戶端標(biāo)識(shí)符,當(dāng)這個(gè)控件生成到客戶端頁面的時(shí)候,在客戶端代碼訪問該控件時(shí)就需要通過ClientID來訪問。
UniqueID 服務(wù)器控件的唯一的、分層的形式限定的標(biāo)識(shí)符。 是當(dāng)需要參與服務(wù)端回傳的時(shí)候用的。當(dāng)將控件放置到重復(fù)控件(Repeater、DataList和DataGrid)中時(shí),將可能生成多個(gè)服務(wù)器端的控件,這就需要區(qū)分服務(wù)器端的各個(gè)控件,以使它們的 ID 屬性不沖突。UniqueID 通過將子控件的父控件的 UniqueID 值與控件的 ID 值連接生成,各個(gè)部分之間以 IdSeparator 屬性指定的字符連接。默認(rèn)情況下, IdSeparator 屬性為冒號(hào)字符 (:)。此屬性為在 .Net Framework2.0種新增加。 (UniqueID 的解釋做了參照,本篇重點(diǎn)強(qiáng)調(diào)ID與ClientID)
2、代碼示例解說
看看如下代碼:
復(fù)制代碼 代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<script type="text/javascript">
function GetValue()
{
<SPAN style="COLOR: #0080c0"><STRONG>var t=document.getElementById('<%= TextBox1.ClientID %>');</STRONG></SPAN>
t.innerText=2;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
<input type="button" runat="server" id="button1" onclick="GetValue();" value="賦值" />
</form>
</body>
</html>

有人會(huì)問了:var t=document.getElementById("TextBox1");不是也運(yùn)行的好好的嗎?
答案:在一般的aspx中ID=ClientID(前提是你自己已經(jīng)設(shè)置好了ID值)
看下面代碼,設(shè)置了模板頁
復(fù)制代碼 代碼如下:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function GetValue()
{
<SPAN style="COLOR: #0080c0"><STRONG>document.write('<%= TextBox1.ClientID %>')</STRONG></SPAN>
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" runat="server" id="button1" onclick="GetValue();" value="賦值" />
</asp:Content>
頁面顯示了<SPAN style="COLOR: #0080c0"><STRONG>ctl00_ContentPlaceHolder1_TextBox1</STRONG></SPAN>。即TextBox1.ClientID =ctl00_ContentPlaceHolder1_TextBox1。

此時(shí)把代碼改成
復(fù)制代碼 代碼如下:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function GetValue()
{
<SPAN style="COLOR: #0080c0"><STRONG><SPAN style="TEXT-DECORATION: line-through">var t=document.getElementById("TextBox1");</SPAN></STRONG></SPAN>
t.innerText=2;
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" runat="server" id="button1" onclick="GetValue();" value="賦值" />
</asp:Content>
出錯(cuò)了,t=null,也就是找不到TextBox1,所以需要改成<SPAN style="COLOR: #0080c0"><STRONG>var t=document.getElementById('<%=TextBox1.ClientID%>');</STRONG></SPAN>

3、綜述
view sourceprint?1 對(duì)于服務(wù)器控件,在客戶端調(diào)時(shí)使用ClientID屬性,在服務(wù)端時(shí)使用ID屬性。

相關(guān)文章

最新評(píng)論