日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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

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

一 :簡介

我們知道因為在原來的ASP.NET應(yīng)用程序中使用服務(wù)端控件在生成ClientID的時,是很難控制的,特別是在嵌套的控件的時候,比如在多個嵌套Repeater中要控制某一個控件生成的html的ID屬性,是很困難的,

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

二 :原來的問題和解決方法

原來要獲得html的ID,就要使用這樣的方式:

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

如果是在嵌套的控件中,就需要使用并接字符串來組合成一下客戶端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.           } 

其實也可以通過重寫控件的ID來,控制在客戶端ID的生成。

三:ASP.NET 4.0 的解決方案

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

他有四個值分別是:

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

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

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

Predictable:這個設(shè)置值的使用,需要確保ID的是唯一性,這里分整個頁面的唯一性和在控件中的唯一性兩種情況,第二中就是說你可以在頁面設(shè)置一個ID為Name,你還是可以在你的Repeater的Item項目模板中設(shè)置ID為Name的Label子控件,而不會報錯,因為他會自動生成新的控件ID。具體下面詳細解說:

(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ù)綁定控件中使用子控件時,他們生成的子控件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.  

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

如果在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"> 

    看來還比較靈活,

    現(xiàn)在我們再在repeater外面方一個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é)果在頁面上就會出現(xiàn)

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

      但并沒有報錯。

      如果在再外面加一個Label,ID為lblName_0的,ClientIDMode為Static或Predictable,就會出現(xiàn)報錯。

       
       
       
      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.  

        你可以看見我們將ProductName作為后綴名。

        新特性總結(jié)

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


        網(wǎng)頁名稱:ASP.NET4.0新特性ClientID的改進
        本文URL:http://m.5511xx.com/article/dhcigec.html

            sfsd td> 
          •           tr> 
          •           HeaderTemplate> 
          •           > 
          •            
          •  
          •                ID="lblID"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
          •              td> tr> 
          •             
          •  
          •                ID="lblName"  Text='<%# Eval("ProductName") %>' runat="server" ClientIDMode="Predictable"> asp:Label> 
          •              td> tr> 
          •             
          •  
          •                ID="lblReorderLevel"  Text='<%# Eval("ReorderLevel")%>' runat="server" ClientIDMode="Predictable"> asp:Label> 
          •              td> tr> 
          •           ItemTemplate> 
          •           
          •           FooterTemplate>     
          •          asp:Repeater> 
          • 這個時候就會報錯,有相同的ClientID。

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

            (3)Predictable Mode

            在GridView數(shù)據(jù)綁定控件中還有一個新的屬性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é)果為:

             
             
             
             
          •                         Chai span> 
          •                          
          •                      td> 
          •          tr>
          •  
          •                         Chang span> 
          •                          
          •                      td> 
          •          tr>
          •  
          •                         Aniseed Syrup span> 
          •                          
          •                      td> 
          •          tr>
          •  
          •                         Chef Anton's Cajun Seasoning span> 
          •                          
          •                      td> 
          •          tr>