日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
ASP.NET4.0新特性ClientID的改進(jìn)

ASP.NET 4.0新特性中,關(guān)于ClientID的改進(jìn)可以在執(zhí)行嵌套空間時(shí),控制生成的Html的ID的情況。以往進(jìn)行這樣的操作時(shí),很容易出現(xiàn)錯(cuò)誤,很難控制。

創(chuàng)新互聯(lián)建站始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營(yíng)理念,通過(guò)多達(dá)10年累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)營(yíng)銷推廣解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:OPP膠袋等企業(yè),備受客戶夸獎(jiǎng)。

一 :簡(jiǎn)介

我們知道因?yàn)樵谠瓉?lái)的ASP.NET應(yīng)用程序中使用服務(wù)端控件在生成ClientID的時(shí),是很難控制的,特別是在嵌套的控件的時(shí)候,比如在多個(gè)嵌套R(shí)epeater中要控制某一個(gè)控件生成的html的ID屬性,是很困難的,

在ASP.NET 4.0新特性中提供ClientMode,來(lái)控制生成的Html的ID的情況。

二 :原來(lái)的問(wèn)題和解決方法

原來(lái)要獲得html的ID,就要使用這樣的方式:

 
 
 
  1. <%=lblName.ClientID%> 
  2.  
  3.  var lblName = document.getElementById("<%=lblName.ClientID%>");  
  4.            alert(lblName.innerText); 

如果是在嵌套的控件中,就需要使用并接字符串來(lái)組合成一下客戶端ID,

 
 
 
  1. for (var i = 1; i <= 9; i++) {  
  2.  
  3.               var Element = document.getElementById("Repeater1_ctl0" + i + "_lblName");  
  4.  
  5.               alert(Element.innerText);  
  6.           } 

其實(shí)也可以通過(guò)重寫(xiě)控件的ID來(lái),控制在客戶端ID的生成。

三:ASP.NET 4.0 的解決方案

現(xiàn)在你會(huì)發(fā)現(xiàn)在ASP.NET 4.0中會(huì)有一個(gè)ClientMode的新屬性:

他有四個(gè)值分別是:

Legacy:就是使用傳統(tǒng)的模式,設(shè)置ClientIDMode是無(wú)效的。

Inherit:這是繼承在控件層次結(jié)構(gòu)中,父級(jí)點(diǎn)控件的ClientIDMode設(shè)置。也就是說(shuō)如果你父控件設(shè)置ClientIDMode=“Static”,那這里的子控件的ClientIDMode也是"Static"

Static :生成指定的ID,但你要注意頁(yè)面上的ClientID的唯一性。

Predictable:這個(gè)設(shè)置值的使用,需要確保ID的是唯一性,這里分整個(gè)頁(yè)面的唯一性和在控件中的唯一性兩種情況,第二中就是說(shuō)你可以在頁(yè)面設(shè)置一個(gè)ID為Name,你還是可以在你的Repeater的Item項(xiàng)目模板中設(shè)置ID為Name的Label子控件,而不會(huì)報(bào)錯(cuò),因?yàn)樗麜?huì)自動(dòng)生成新的控件ID。具體下面詳細(xì)解說(shuō):

(1)使用Legacy 值:

 
 
 
  1.  :TextBox ID ="txtName" runat ="server" Width ="70%" ClientIDMode ="Legacy" /> 
  2.  id="ctl00_txtName" style="width: 65%" name="ctl00$txtName" /> 

上面是和傳統(tǒng)生成 Client ID的情況的一樣。

(2)Static 模式

ClientIDMode的值設(shè)置為Static,這里要注意就是在repeater等數(shù)據(jù)綁定控件中使用子控件時(shí),他們生成的子控件ID都是一樣的,所以控制不好控制。

 
 
 
  1.  
  2.  
  3.  id="lblName"> 
  4. td> 
  5.  
  6.  
  7.  
  8.  id="lblName"> 
  9. td> 
  10.  
  11.  
  12.  
  13.  id="lblName"> 
  14. td> 
  15.  

所以可以看出它比較適合單個(gè)控件的使用。

如果在repeater設(shè)置為Static,而將后面的控件設(shè)為Predictable

 
 
 
  1.  ID="SqlDataSource1" runat="server"   
  2.            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"   
  3.            SelectCommand="SELECT * FROM [Products]"> asp:SqlDataSource> 
  4.         ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" ClientIDMode="Static"> 
  5.          > 
  6.          
  7.         
  8.  
  9.            
  10. sfsd td> 
  11.          tr> 
  12.          HeaderTemplate> 
  13.          > 
  14.           
  15.  
  16.               ID="lblID"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
  17.             td> tr> 
  18.            
  19.  
  20.               ID="lblName"  Text='<%# Eval("ProductName") %>' runat="server" ClientIDMode="Predictable"> asp:Label> 
  21.             td> tr> 
  22.            
  23.  
  24.               ID="lblReorderLevel"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
  25.             td> tr> 
  26.          ItemTemplate> 
  27.          
  28.          FooterTemplate>     
  29.         asp:Repeater> 
  30. 結(jié)果為:

     
     
     
    1.  id="lblName_0"> 
    2.  id="lblName_1"> 
    3.  id="lblName_2"> 
    4.  id="lblName_3"> 

    看來(lái)還比較靈活,

    現(xiàn)在我們?cè)僭趓epeater外面方一個(gè)Label,ID為lblName_0的,ClientIDMode為Static或Predictable;

     
     
     
    1.    ID="lblName_0"  Text="worksguo" runat="server" ClientIDMode=“Static或Predictable”> asp:Label> 
    2.          ID="SqlDataSource1" runat="server"   
    3.             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"   
    4.             SelectCommand="SELECT * FROM [Products]"> asp:SqlDataSource> 
    5.          ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" ClientIDMode="Static"> 
    6.           > 
    7.           
    8.          
    9.  
    10.             
    11. sfsd td> 
    12.           tr> 
    13.           HeaderTemplate> 
    14.           > 
    15.            
    16.  
    17.                ID="lblID"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
    18.              td> tr> 
    19.             
    20.  
    21.                ID="lblName"  Text='<%# Eval("ProductName") %>' runat="server" ClientIDMode="Predictable"> asp:Label> 
    22.              td> tr> 
    23.             
    24.  
    25.                ID="lblReorderLevel"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
    26.              td> tr> 
    27.           ItemTemplate> 
    28.           
    29.           FooterTemplate>     
    30.          asp:Repeater> 
    31. 結(jié)果在頁(yè)面上就會(huì)出現(xiàn)

       
       
       
      1.  id="lblName_0"> 
      2.  id="lblName_0"> 

      但并沒(méi)有報(bào)錯(cuò)。

      如果在再外面加一個(gè)Label,ID為lblName_0的,ClientIDMode為Static或Predictable,就會(huì)出現(xiàn)報(bào)錯(cuò)。

       
       
       
      1.    ID="lblName_0"  Text="worksguo" runat="server"> asp:Label> 
      2.       ID="lblName_0"  Text="worksguo" runat="server"> asp:Label> 
      3.          ID="SqlDataSource1" runat="server"   
      4.             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"   
      5.             SelectCommand="SELECT * FROM [Products]"> asp:SqlDataSource> 
      6.          ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" ClientIDMode="Static"> 
      7.           > 
      8.           
      9.          
      10.  
      11.             
      12.  
      13.             ProductName th> 
      14.          tr>
      15.  
      16.             
      17.  
      18.             
      19.  
      20.             
      21.  
      22.             
      23.  

        你可以看見(jiàn)我們將ProductName作為后綴名。

        新特性總結(jié)

        現(xiàn)在有這個(gè)ClientMode就能很好的控制生成到客戶端的ID,這樣可以更好的動(dòng)態(tài)控制頁(yè)面上標(biāo)簽。


        當(dāng)前題目:ASP.NET4.0新特性ClientID的改進(jìn)
        文章轉(zhuǎn)載:http://www.dlmjj.cn/article/dhcigec.html
        sfsd td> 
      24.           tr> 
      25.           HeaderTemplate> 
      26.           > 
      27.            
      28.  
      29.                ID="lblID"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
      30.              td> tr> 
      31.             
      32.  
      33.                ID="lblName"  Text='<%# Eval("ProductName") %>' runat="server" ClientIDMode="Predictable"> asp:Label> 
      34.              td> tr> 
      35.             
      36.  
      37.                ID="lblReorderLevel"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
      38.              td> tr> 
      39.           ItemTemplate> 
      40.           
      41.           FooterTemplate>     
      42.          asp:Repeater> 
      43. 這個(gè)時(shí)候就會(huì)報(bào)錯(cuò),有相同的ClientID。

        所以ClientIDMode使用是有層次范圍的,在頁(yè)面上相同層次級(jí)別上不能有相同ID,如果在Repeater中新的層次中就可以與上一層次有相同ID.

        (3)Predictable Mode

        在GridView數(shù)據(jù)綁定控件中還有一個(gè)新的屬性ClientIDRowSuffix,它是在GridView中設(shè)置在使用Predictable模式,生成新的ID的后綴。

         
         
         
        1.  ID="GridView1" runat="server" AutoGenerateColumns="False"   
        2.             DataKeyNames="ProductName" DataSourceID="SqlDataSource1" ClientIDMode="Predictable" ClientIDRowSuffix="ProductName" > 
        3.              
        4.                  HeaderText="ProductName" > 
        5.                      
        6.                            ID="lblID"  Text='<%# Eval("ProductName")%>' runat="server" > asp:Label> 
        7.                          
        8.                      ItemTemplate> 
        9.                  asp:TemplateField>      
        10.              Columns> 
        11.          asp:GridView> 

        生成的結(jié)果為:

         
         
         
         
      44.                         Chai span> 
      45.                          
      46.                      td> 
      47.          tr>
      48.  
      49.                         Chang span> 
      50.                          
      51.                      td> 
      52.          tr>
      53.  
      54.                         Aniseed Syrup span> 
      55.                          
      56.                      td> 
      57.          tr>
      58.  
      59.                         Chef Anton's Cajun Seasoning span> 
      60.                          
      61.                      td> 
      62.          tr>