11.代 :從列表控件中移除項(xiàng) (Visual C#)
本示例使用 ListBox.Items 屬性的 Remove 方法移除從列表中移除項(xiàng)。
示例:
ListBox1.Items.Remove("Mary");
編譯代 -----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并執(zhí)行以下操作:
ListBox1
的列表框并在其中填充項(xiàng)。
可 編程-----》將"Mary"
替換為所要移除的項(xiàng)的值。
12.代 :從視圖狀態(tài)檢索數(shù)據(jù)集 (Visual C#)
本示例將從視圖狀態(tài)檢索數(shù)據(jù)集。
示例:
if (Page.IsPostBack) { System.IO.StringReader sr = new System.IO.StringReader((string)(ViewState["dSet"])); dSet.ReadXml(sr); }
編譯代 ----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并執(zhí)行以下操作:
13.代 :重定向至其他頁 (Visual C#)
本示例使用 HttpResponse.Redirect 方法將瀏覽器重定向到指定的 URL。
示例:
Response.Redirect(http://www.microsoft.com/china);
編譯代 -----》啟動(dòng)新的 ASP.NET 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
14.代 :重定向到同一應(yīng)用程序中的其他頁 (Visual C#)
本示例使用 Server.Transfer 方法將瀏覽器重定向到指定頁。
示例:
Server.Transfer("WebForm2.aspx");
編譯代
15.代 :使用應(yīng)用程序狀態(tài)保存值 (Visual C#)
本示例使用 HttpApplicationState 類保存全局信息。
示例:
Application["Message"] = "AppMsg"; Application["AppStartTime"] = DateTime.Now;
編譯代 -----》啟動(dòng)新的 ASP.NET 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
16.代 :使用會(huì)話狀態(tài)保存值 (Visual C#)
本示例使用 Session.Add 方法在單個(gè)會(huì)話內(nèi)保持值。
示例:
string firstName = "Jeff"; string lastName = "Smith"; string city = "Seattle"; Session.Add("First", firstName); Session.Add("Last", lastName); Session.Add("City", city);
編譯代 -----》啟動(dòng)新的 ASP.NET 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
17.代 :將數(shù)據(jù)集保存到視圖狀態(tài)中 (Visual C#)
本示例使用 Control.ViewState 屬性保存數(shù)據(jù)集。
示例
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();
編譯代 -----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并執(zhí)行以下操作:
18.代 :使用視圖狀態(tài)保存值 (Visual C#)
本示例將字符串值 "yellow"
保存到 Control.ViewState 屬性中。示例
ViewState["color"] = "yellow";
編譯代 -----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
19.代 :選擇列表控件中的項(xiàng) (Visual C#)
本示例使用 IndexOf 方法在列表框中搜索特定文本。
示例
ListBox1.SelectedIndex = ListBox1.Items.IndexOf(ListBox1.Items.FindByText("ValueToFind"));
編譯代 -----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并執(zhí)行以下操作:
ListBox1
的列表框并在其中填充項(xiàng)。
20.代 :編寫 Cookie (Visual C#)
下面的示例將使用 HttpCookie 類及其屬性編寫一個(gè)一分鐘后將失效的 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.");
編譯代 -----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
來源:http://msdn.microsoft.com/zh-cn/library/aa287564(VS.71).aspx
聯(lián)系客服