同一頁面.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"];
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"];
<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"];
protected void Button1_Click(object sender, EventArgs e)
{
Session["username"] = "a";
Request.Redirect("receive.aspx");
}
receive.aspx:
string username = Session["username"];