溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET 4.0新特性的改進有哪些

發布時間:2021-11-26 17:40:58 來源:億速云 閱讀:150 作者:柒染 欄目:編程語言

這期內容當中小編將會給大家帶來有關ASP.NET 4.0新特性的改進有哪些,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

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

一 :簡介

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

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

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

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

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

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

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

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

三:ASP.NET 4.0 的解決方案

現在你會發現在ASP.NET 4.0中會有一個ClientMode的新屬性:

他有四個值分別是:

Legacy:就是使用傳統的模式,設置ClientIDMode是無效的。

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

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

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

(1)使用Legacy 值:

<asp :TextBox ID ="txtName" runat ="server" Width ="70%" ClientIDMode ="Legacy" /> <input id="ctl00_txtName" style="width: 65%" name="ctl00$txtName" />

上面是和傳統生成 Client ID的情況的一樣。

(2)Static 模式

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

<tr> <td> <span id="lblName"> td> <tr> <tr> <td> <span id="lblName"> td> <tr> <tr> <td> <span id="lblName"> td> <tr>


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

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

<asp:SqlDataSource ID="SqlDataSource1" runat="server"              ConnectionString=""              SelectCommand="SELECT * FROM [Products]">asp:SqlDataSource>        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" ClientIDMode="Static">         <HeaderTemplate >         <table>         <tr>            <td>sfsdtd>         tr>         HeaderTemplate>         <ItemTemplate >           <tr><td>            <asp:Label   ID="lblID"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>            td>tr>            <tr><td>            <asp:Label   ID="lblName"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>            td>tr>            <tr><td>            <asp:Label   ID="lblReorderLevel"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>            td>tr>         ItemTemplate>         <FooterTemplate>         FooterTemplate>            asp:Repeater>

結果為:

<span id="lblName_0"> <span id="lblName_1"> <span id="lblName_2"> <span id="lblName_3">

看來還比較靈活,

現在我們再在repeater外面方一個Label,ID為lblName_0的,ClientIDMode為Static或Predictable;

<asp:Label   ID="lblName_0"  Text="worksguo" runat="server" ClientIDMode=“Static或Predictable”>asp:Label>         <asp:SqlDataSource ID="SqlDataSource1" runat="server"               ConnectionString=""               SelectCommand="SELECT * FROM [Products]">asp:SqlDataSource>         <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" ClientIDMode="Static">          <HeaderTemplate >          <table>          <tr>             <td>sfsdtd>          tr>          HeaderTemplate>          <ItemTemplate >            <tr><td>             <asp:Label   ID="lblID"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>             td>tr>             <tr><td>             <asp:Label   ID="lblName"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>             td>tr>             <tr><td>             <asp:Label   ID="lblReorderLevel"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>             td>tr>          ItemTemplate>          <FooterTemplate>          FooterTemplate>             asp:Repeater>

結果在頁面上就會出現

<span id="lblName_0"> <span id="lblName_0">

但并沒有報錯。

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

<asp:Label   ID="lblName_0"  Text="worksguo" runat="server">asp:Label>    <asp:Label   ID="lblName_0"  Text="worksguo" runat="server">asp:Label>         <asp:SqlDataSource ID="SqlDataSource1" runat="server"               ConnectionString=""               SelectCommand="SELECT * FROM [Products]">asp:SqlDataSource>         <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" ClientIDMode="Static">          <HeaderTemplate >          <table>          <tr>             <td>sfsdtd>          tr>          HeaderTemplate>          <ItemTemplate >            <tr><td>             <asp:Label   ID="lblID"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>             td>tr>             <tr><td>             <asp:Label   ID="lblName"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>             td>tr>             <tr><td>             <asp:Label   ID="lblReorderLevel"  Text='' runat="server" ClientIDMode="Predictable">asp:Label>             td>tr>          ItemTemplate>          <FooterTemplate>          FooterTemplate>             asp:Repeater>

這個時候就會報錯,有相同的ClientID。

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

(3)Predictable Mode

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

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"               DataKeyNames="ProductName" DataSourceID="SqlDataSource1" ClientIDMode="Predictable" ClientIDRowSuffix="ProductName" >             <Columns>                 <asp:TemplateField HeaderText="ProductName" >                     <ItemTemplate>                         <asp:Label   ID="lblID"  Text='' runat="server" >asp:Label>                                              ItemTemplate>                 asp:TemplateField>                  Columns>         asp:GridView>


生成的結果為:

<tr>             <th scope="col">ProductNameth>         tr><tr>             <td>                         <span id="GridView1_lblID_Chai">Chaispan>                                              td>         tr><tr>             <td>                         <span id="GridView1_lblID_Chang">Changspan>                                              td>         tr><tr>             <td>                         <span id="GridView1_lblID_Aniseed Syrup">Aniseed Syrupspan>                                              td>         tr><tr>             <td>                         <span id="GridView1_lblID_Chef Anton's Cajun Seasoning">Chef Anton's Cajun Seasoningspan>                                              td>         tr><tr>

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

新特性總結

現在有這個ClientMode就能很好的控制生成到客戶端的ID,這樣可以更好的動態控制頁面上標簽。

上述就是小編為大家分享的ASP.NET 4.0新特性的改進有哪些了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女