日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Visual?C#?Web?應(yīng)用程序示例主題(一)(MSDN整理)

 悟靜 2012-08-22
1.代碼:在運(yùn)行時(shí)添加控件 (Visual C#)

本示例在運(yùn)行時(shí)將文本框和按鈕添加到 Web 窗體頁(yè)。它還將事件處理程序動(dòng)態(tài)綁定到按鈕的 Click 事件。該處理程序會(huì)顯示動(dòng)態(tài)生成的文本框的值。

這些控件被添加到 Panel Web 服務(wù)器控件中,該控件是一個(gè)占位符。在面板中使用分行符(HTML
元素)來(lái)分隔這些控件,它們被添加到使用 LiteralControl 控件的面板中。

示例:

private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
        AddControls();
}
protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    if (ViewState["controsladded"] == null)
    AddControls();
}
private void AddControls()
{
    TextBox dynamictextbox = new TextBox();
    dynamictextbox.Text = "(Enter some text)";
    dynamictextbox.ID = "dynamictextbox";
    Button dynamicbutton = new Button();
    dynamicbutton.Click += new System.EventHandler(dynamicbutton_Click);
    dynamicbutton.Text = "Dynamic Button";
    Panel1.Controls.Add(dynamictextbox);
    Panel1.Controls.Add(new LiteralControl("
"));
    Panel1.Controls.Add(new LiteralControl("
"));
    Panel1.Controls.Add(dynamicbutton);
    ViewState["controlsadded"] = true;
}
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
    TextBox tb = new TextBox();
    tb = (TextBox) (Panel1.FindControl("dynamictextbox"));
    Label1.Text = tb.Text;
}

編譯代碼-----》本示例需要: Web 窗體頁(yè)。 稱作 Panel1 的 Panel 控件。 稱作 Label1 的 Label 控件。

2.代碼:將項(xiàng)添加到列表控件 (Visual C#)

本示例使用 ListBox.Items 屬性Add 方法向列表中添加項(xiàng)。

示例:

// Create a string array and assign items.
string[] ListContents = {"Enju", "Sue", "Mary"};
// Add the array items to the list box.
for (int i = 0; i <= ListContents.Length - 1; i++)
   ListBox1.Items.Add(ListContents[i]);

編譯代碼------》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并執(zhí)行以下操作: 

        ------》添加名為 ListBox1 的列表框。 

       ------》復(fù)制代碼并將其粘貼到 Page_Load 方法中。

3.代碼:檢測(cè)瀏覽器的版本 (Visual C#)

本示例將檢查 Request.Browser 屬性以確定客戶端 Web 瀏覽器的類型和版本。通過(guò) Request.Browser 屬性還可以訪問(wèn)瀏覽器的其他許多屬性,如 Url、ServerVariablesUser

示例:
HttpBrowserCapabilities brObject = Request.Browser;
// Display the type and version.
Response.Write("Browser Type: "+ brObject.Type);
Response.Write("

"+"Browser Version: "+ brObject.Version);

編譯代碼-----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并將代碼粘貼到 Page_Load 方法中。

4.代碼:確定列表控件中所選的項(xiàng) (Visual C#)

本示例使用列表控件的 SelectedIndexSelectedItem 屬性訪問(wèn)有關(guān)所選項(xiàng)的信息。

示例:

// The index:
int ListItemIndex;
// Value of the item:
string ListItemValue;
ListItemIndex = ListBox1.SelectedIndex;
ListItemValue = ListBox1.SelectedItem.Value.ToString();

 編譯代碼-----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并執(zhí)行以下操作:

       添加名為 ListBox1 的列表框并在其中填充項(xiàng)。

       復(fù)制代碼并將其粘貼到 Page_Load 方法中。

5.代碼:顯示 Web 窗體 DataGrid 控件中的數(shù)據(jù) (Visual C#)

本示例使用 DataBind 方法將數(shù)據(jù)集的內(nèi)容綁定到 DataGrid 控件。

示例:

if (!IsPostBack)
{
    sqlDataAdapter1.Fill(dataSet11);
    DataGrid1.DataBind();
}

編譯代碼

本示例需要:




  • Web 窗體頁(yè)。
  • 名為 DataGrid1DataGrid 控件。
  • 名為 dataSet11 的類型化數(shù)據(jù)集,該數(shù)據(jù)集是類型化數(shù)據(jù)集類 DataSet1 的實(shí)例。
  • 正確配置的名為 sqlDataAdapter1 的數(shù)據(jù)適配器??梢詫?DataGrid 控件綁定到任何 Web 窗體頁(yè)數(shù)據(jù)源。
  • 應(yīng)用程序必須具有訪問(wèn)數(shù)據(jù)庫(kù)的權(quán)限。

6.代碼:將數(shù)據(jù)從一個(gè) Web 窗體頁(yè)傳遞到另一個(gè) Web 窗體頁(yè) (Visual C#)

本示例保存會(huì)話狀態(tài)中的信息并定位到同一應(yīng)用程序中的另一頁(yè),然后在第二頁(yè)中檢索存儲(chǔ)的信息。

示例:

// First part: Saves info on first page. string firstName = "John"; string lastName = "Smith"; string city = "Seattle"; Session.Add("First", firstName); Session.Add("Last", lastName); Session.Add("City", city); // Redirect to second page. Server.Transfer("WebForm2.aspx"); // Second part: Reads info into variables on the second page. string firstName = (string)(Session["First"]); string lastName = (string)(Session["Last"]); string city = (string)(Session["City"]); 
編譯代

  • 啟動(dòng)具有兩個(gè) Web 窗體頁(yè)(WebForm1.aspx 和 WebForm2.aspx)的 ASP.NET 應(yīng)用程序。
  • 將代碼的第一部分粘貼到 WebForm1.aspx 的 Page_Load 方法(或 Click 方法)中。
  • 將代碼的第二部分粘貼到 WebForm2.aspx Page_Load 方法中。

7.代碼:讀取 Cookie (Visual C#)

下面示例使用 HttpCookie 類及其屬性讀取具有特定名稱的 Cookie。

示例:

HttpCookie myCookie = new HttpCookie("MyTestCookie"); myCookie = Request.Cookies["MyTestCookie"]; // Read the cookie information and display it. if (myCookie != null) Response.Write("
"+ myCookie.Name + "
"+ myCookie.Value); else Response.Write("not found");
編譯代 -----》啟動(dòng)新的 ASP.NET Web 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
8.代 :讀取應(yīng)用程序狀態(tài)中的值 (Visual C#)  

本示例從 HttpApplicationState 對(duì)象檢索值。

示例:

string appMsg; appMsg = (string)(Application["Message"]); 
編譯代 -----》啟動(dòng)新的 ASP.NET 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
可 編程-----》

必須將數(shù)據(jù)轉(zhuǎn)換成適當(dāng)?shù)念愋停ㄈ绱死兴荆?BR>如果嘗試從不存在的應(yīng)用程序狀態(tài)中獲取值,則不會(huì)引發(fā)任何異常。若要確保所需的值在應(yīng)用程序狀態(tài)中,請(qǐng)首先使用測(cè)試(例如以下測(cè)試)檢查該對(duì)象是否存在:
if (Application["Message"] == null) Response.Write("No such value in application state."); 
9.代 :讀取會(huì)話狀態(tài)中的值 (Visual C#)  

本示例訪問(wèn) HttpSessionState.Item 屬性來(lái)檢索處于會(huì)話狀態(tài)的值。

示例:

string firstName = (string)(Session["First"]); string lastName = (string)(Session["Last"]); string city = (string)(Session["City"]); 
編譯代 -----》啟動(dòng)新的 ASP.NET 應(yīng)用程序并將代 粘貼到 Page_Load 方法中。
可 編程-----》

會(huì)話值的類型為 Object。讀取它們時(shí)應(yīng)將其轉(zhuǎn)換成適當(dāng)?shù)念愋汀?/P>

如果嘗試從不存在的會(huì)話狀態(tài)中獲取值,則不會(huì)引發(fā)任何異常。若要確保所需的值在會(huì)話狀態(tài)中,請(qǐng)首先使用測(cè)試(例如以下測(cè)試)檢查該對(duì)象是否存在:

if (Session["City"] == null) Response.Write("No such value in session state."); 
10.代 :從視圖狀態(tài)中讀取值 (Visual C#)  

本示例將值 "yellow" 保存到 Control.ViewState 屬性中,然后當(dāng)該頁(yè)回發(fā)到服務(wù)器時(shí)檢索該屬性的值。有關(guān) ViewState 的更多信息,請(qǐng)參見(jiàn)使用視圖狀態(tài)保存 Web 窗體頁(yè)值。

示例:

string strColor; if (Page.IsPostBack) { // Retrieve and display the property value. strColor = (string)ViewState["color"]; Response.Write(strColor); } else // Save the property value. ViewState["color"] = "yellow"; 
編譯代 -----》復(fù)制代 并將其粘貼至 ASP.NET Web 應(yīng)用程序的 Page_Load 方法中。
 來(lái)源:http://msdn.microsoft.com/zh-cn/library/aa287564(VS.71).aspx 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多