人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開(kāi)發(fā)教程 > 詳細(xì)頁(yè)面

ASP.NET ViewState 初探 (2)

ASP.NET ViewState 初探 (2)

更新時(shí)間:2022-09-12 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):

請(qǐng)看下面的示例:要在 Web 頁(yè)上顯示一個(gè)項(xiàng)目列表,而每個(gè)用戶需要不同的列表排序。項(xiàng)目列表是靜態(tài)的,因此可以將這些頁(yè)面綁定到相同的緩存數(shù)據(jù)集,而排序順序只是用戶特定的 UI 狀態(tài)的一小部分。ViewState 非常適合于存儲(chǔ)這種類型的值。代碼如下:

[Visual Basic]
<%@ Import Namespace="System.Data" %>
<HTML>
<HEAD>
<title>用于頁(yè)面 UI 狀態(tài)值的 ViewState/title>
</HEAD>
<body>
<form runat="server">
<H3>
在 ViewState 中存儲(chǔ)非控件狀態(tài)
</H3>
<P>
此示例將一列靜態(tài)數(shù)據(jù)的當(dāng)前排序順序存儲(chǔ)在 ViewState 中。<br>
單擊列標(biāo)題中的鏈接,可按該字段排序數(shù)據(jù)。<br>
再次單擊該鏈接,將按相反順序排序。
<br><br><br>
<asp:datagrid id="DataGrid1" runat="server"
OnSortCommand="SortGrid" BorderStyle="None" BorderWidth="1px"
BorderColor="#CCCCCC" BackColor="White" CellPadding="5" AllowSorting="True">
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#006699">
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat="server">

' 在 ViewState 中跟蹤 SortField 屬性
Property SortField() As String

Get
Dim o As Object = ViewState("SortField")
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get

Set(Value As String)
If Value = SortField Then
' 與當(dāng)前排序文件相同,切換排序方向
SortAscending = Not SortAscending
End If
ViewState("SortField") = Value
End Set

End Property

' 在 ViewState 中跟蹤 SortAscending 屬性
Property SortAscending() As Boolean

Get
Dim o As Object = ViewState("SortAscending")
If o Is Nothing Then
Return True
End If
Return CBool(o)
End Get

Set(Value As Boolean)
ViewState("SortAscending") = Value
End Set

End Property

Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
BindGrid()
End If

End Sub

Sub BindGrid()

' 獲取數(shù)據(jù)
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath("TestData.xml"))

Dim dv As New DataView(ds.Tables(0))

' 應(yīng)用排序過(guò)濾器和方向
dv.Sort = SortField
If Not SortAscending Then
dv.Sort += " DESC"
End If

' 綁定網(wǎng)格
DataGrid1.DataSource = dv
DataGrid1.DataBind()

End Sub

Private Sub SortGrid(sender As Object, e As DataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex = 0
SortField = e.SortExpression
BindGrid()
End Sub

</script>

[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<HEAD>
<title>用于頁(yè)面 UI 狀態(tài)值的 ViewState</title>
</HEAD>
<body>
<form runat="server">
<H3>
在 ViewState 中存儲(chǔ)非控件狀態(tài)
</H3>
<P>
此示例將一列靜態(tài)數(shù)據(jù)的當(dāng)前排序順序存儲(chǔ)在 ViewState 中。<br>
單擊列標(biāo)題中的鏈接,可按該字段排序數(shù)據(jù)。<br>
再次單擊該鏈接,將按相反順序排序。
<br><br><br>
<asp:datagrid id="DataGrid1" runat="server" OnSortCommand="SortGrid"
BorderStyle="None" BorderWidth="1px" BorderColor="#CCCCCC"
BackColor="White" CellPadding="5" AllowSorting="True">
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699">
</HeaderStyle>
</asp:datagrid>
</P>
</form>
</body>
</HTML>
<script runat="server">

// 在 ViewState 中跟蹤 SortField 屬性
string SortField {

get {
object o = ViewState["SortField"];
if (o == null) {
return String.Empty;
}
return (string)o;
}

set {
if (value == SortField) {
// 與當(dāng)前排序文件相同,切換排序方向
SortAscending = !SortAscending;
}
ViewState["SortField"] = value;
}
}

// 在 ViewState 中跟蹤 SortAscending 屬性
bool SortAscending {

get {
object o = ViewState["SortAscending"];
if (o == null) {
return true;
}
return (bool)o;
}

set {
ViewState["SortAscending"] = value;
}
}

void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {
BindGrid();
}
}

void BindGrid() {

// 獲取數(shù)據(jù)
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("TestData.xml"));

DataView dv = new DataView(ds.Tables[0]);

// 應(yīng)用排序過(guò)濾器和方向
dv.Sort = SortField;
if (!SortAscending) {
dv.Sort += " DESC";
}

// 綁定網(wǎng)格
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}

void SortGrid(object sender, DataGridSortCommandEventArgs e) {

DataGrid1.CurrentPageIndex = 0;
SortField = e.SortExpression;
BindGrid();
}

</script>

下面是上述兩個(gè)代碼段中引用的 testdata.xml 的代碼:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<pub_id>0736</pub_id>
<pub_name>New Moon Books</pub_name>
<city>Boston</city>
<state>MA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>0877</pub_id>
<pub_name>Binnet & Hardley</pub_name>
<city>Washington</city>
<state>DC</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1389</pub_id>
<pub_name>Algodata Infosystems</pub_name>
<city>Berkeley</city>
<state>CA</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1622</pub_id>
<pub_name>Five Lakes Publishing</pub_name>
<city>Chicago</city>
<state>IL</state>
<country>USA</country>
</Table>
<Table>
<pub_id>1756</pub_id>
<pub_name>Ramona Publishers</pub_name>
<city>Dallas</city>
<state>TX</state>
<country>USA</country>
</Table>
<Table>
<pub_id>9901</pub_id>
<pub_name>GGG&G</pub_name>
<city>Muenchen</city>
<country>Germany</country>
</Table>
<Table>
<pub_id>9952</pub_id>
<pub_name>Scootney Books</pub_name>
<city>New York</city>
<state>NY</state>
<country>USA</country>
</Table>
<Table>
<pub_id>9999</pub_id>
<pub_name>Lucerne Publishing</pub_name>
<city>Paris</city>
<country>France</country>
</Table>
</NewDataSet>

溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 大渡口区| 金溪县| 兴安县| 宣恩县| 嘉义市| 深泽县| 松溪县| 景德镇市| 丰镇市| 江孜县| 遂宁市| 光泽县| 万州区| 肇东市| 库车县| 五大连池市| 庆安县| 东至县| 周至县| 磐安县| 武鸣县| 平乡县| 新巴尔虎左旗| 应用必备| 东辽县| 全椒县| 胶州市| 通河县| 穆棱市| 阳泉市| 太原市| 阳高县| 定日县| 光山县| 三门峡市| 彭水| 威信县| 即墨市| 东兴市| 五家渠市| 乃东县|