ASP.NET參數傳遞總結

同一頁面.aspx與.aspx.cs之間參數傳遞
1. .aspx.cs接收.aspx的參數:由於.aspx和.aspx.cs爲繼承關係,所以.aspx.cs可以直接對.aspx中的ID進行值提取,具體語句爲string b = a.text; 其中a爲.aspx中的文本框的ID;
2. .aspx接收.aspx.cs的變量:將.aspx.cs的變量設爲全局變量,在.aspx中直接引用<%=a %>,這裏a爲.aspx.cs
中聲明的全局變量;
 不同頁面之間的參數傳遞
1.URL傳遞參數方法,有兩種方法:
  第一種:send.aspx
             <a href=receive.aspx?a=b></a>
                  receive.aspx.cs
            string c = Request.QueryString["a"];
  第二種:send.aspx.cs:
  protected void Button1_Click(object sender, EventArgs e)
    {
        Request.Redirect("receive.aspx?a=b");
    }
                 receive.aspx.cs:
  string username = Request.QueryString["username"];
2. Form表單POST方法
send.aspx
<form id="form1" runat="server" action="receive.aspx" method=post>
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:TextBox ID="a" runat="server"></asp:TextBox>
   </div>
    </form>
receive.aspx.cs
string b = Ruquest.Form["a"];
3.通過session方法傳遞參數
send.aspx.cs:
  protected void Button1_Click(object sender, EventArgs e)
    {
        Session["username"] = "a";
        Request.Redirect("receive.aspx");
    }
 receive.aspx:
 string username = Session["username"];
發佈了47 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章