Visual?C#?Web?應用程序示例主題(二)(MSDN整理)
11.代 :從列表控制項中移除項 (Visual C#) 本示例使用 ListBox.Items 屬性的 Remove 方法移除從列表中移除項。示例:
ListBox1.Items.Remove("Mary");
編譯代 -----》啟動新的 ASP.NET Web 應用程序並執行以下操作:
ListBox1
的列表框並在其中填充項。 可 編程-----》將 "Mary"
替換為所要移除的項的值。 12.代 :從視圖狀態檢索數據集 (Visual C#)本示例將從視圖狀態檢索數據集。示例:
if (Page.IsPostBack) { System.IO.StringReader sr = new System.IO.StringReader((string)(ViewState["dSet"])); dSet.ReadXml(sr); }
編譯代 ----》啟動新的 ASP.NET Web 應用程序並執行以下操作:
13.代 :重定向至其他頁 (Visual C#)
本示例使用 HttpResponse.Redirect 方法將瀏覽器重定向到指定的 URL。
示例:
Response.Redirect(http://www.microsoft.com/china);
編譯代 -----》啟動新的 ASP.NET 應用程序並將代 粘貼到 Page_Load 方法中。
14.代 :重定向到同一應用程序中的其他頁 (Visual C#)本示例使用 Server.Transfer 方法將瀏覽器重定向到指定頁。示例:
Server.Transfer("WebForm2.aspx");
編譯代
15.代 :使用應用程序狀態保存值 (Visual C#)
本示例使用 HttpApplicationState 類保存全局信息。
示例:
Application["Message"] = "AppMsg"; Application["AppStartTime"] = DateTime.Now;
編譯代 -----》啟動新的 ASP.NET 應用程序並將代 粘貼到 Page_Load 方法中。
16.代 :使用會話狀態保存值 (Visual C#)
本示例使用 Session.Add 方法在單個會話內保持值。
示例:
string firstName = "Jeff"; string lastName = "Smith"; string city = "Seattle"; Session.Add("First", firstName); Session.Add("Last", lastName); Session.Add("City", city);
編譯代 -----》啟動新的 ASP.NET 應用程序並將代 粘貼到 Page_Load 方法中。
17.代 :將數據集保存到視圖狀態中 (Visual C#)本示例使用 Control.ViewState 屬性保存數據集。示例
sqlDataAdapter1.Fill(dSet); System.IO.StringWriter sw = new System.IO.StringWriter(); // Write the DataSet to the ViewState property. dSet.WriteXml(sw); ViewState["dSet"] = sw.ToString();
編譯代 -----》啟動新的 ASP.NET Web 應用程序並執行以下操作:
18.代 :使用視圖狀態保存值 (Visual C#)
本示例將字元串值 "yellow"
保存到 Control.ViewState 屬性中。示例
ViewState["color"] = "yellow";
編譯代 -----》啟動新的 ASP.NET Web 應用程序並將代 粘貼到 Page_Load 方法中。
19.代 :選擇列表控制項中的項 (Visual C#) 本示例使用 IndexOf 方法在列表框中搜索特定文本。示例
ListBox1.SelectedIndex = ListBox1.Items.IndexOf(ListBox1.Items.FindByText("ValueToFind"));
編譯代 -----》啟動新的 ASP.NET Web 應用程序並執行以下操作:
ListBox1
的列表框並在其中填充項。 20.代 :編寫 Cookie (Visual C#)
下面的示例將使用 HttpCookie 類及其屬性編寫一個一分鐘後將失效的 Cookie。
示例
HttpCookie myCookie = new HttpCookie("MyTestCookie"); DateTime now = DateTime.Now; // Set the cookie value. myCookie.Value = now.ToString(); // Set the cookie expiration date. myCookie.Expires = now.AddMinutes(1); // Add the cookie. Response.Cookies.Add(myCookie); Response.Write("The cookie has been written.");
編譯代 -----》啟動新的 ASP.NET Web 應用程序並將代 粘貼到 Page_Load 方法中。
來源:http://msdn.microsoft.com/zh-cn/library/aa287564(VS.71).aspx
推薦閱讀: