细说页面传值的N种方式
1 一个页面可以有多个form表单,但是只能有一个runat="server"的form
2 页面传值的7种方式~七剑下天山喽
3 get 和 post的区别 (get请求时,通过url传给服务器的值。post请求时,通过表单发送给服务器的值)
超链接形式&Get形式 :
default.aspx:
default2.aspx.cs:
Post形式:post提交这里写了2种形式的,第一种通过js脚本提交form,第二种form提交。
default.aspx:
default2.aspx.cs:截图在超链接方式的截图中
default4.aspx
<form id="form1" runat="server">
<div>
<asp:TextBox ID="text" runat="server"></asp:TextBox>
</div>
</form>
default4.aspx.cs:
protected void Page_Load(object sender, EventArgs e)Response.Redirect形式
{
if (Request.Form["txtName"] != null)
{
this.text.Text = Request.Form["txtName"];
}
}
defaultNew.aspx:
<p>server控件点击事件响应一个跳转页面的事件</p>default.aspx.cs
<asp:Button ID="btn1" Text="server控件into Default3" runat="server" OnClick="btn1_Click" />
protected void btn1_Click(object sender, EventArgs e)default3.aspx
{
Response.Redirect("Default3.aspx?name=" + btn1.Text.ToString());
}
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
</div>
</form>
default3.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
txt1.Text = Request.QueryString["name"];
string text = Request["name"];
Response.Write("<script>alert('param1=" + text + "')</script>");
}
Application形式 & Session形式 & Server.Transfer
default.aspx:
<p>Application and Session</p>default.aspx.cs:
<asp:Button ID="btn2" Text="application" runat="server" OnClick="btn2_Click" />
<asp:Button ID="btn3" Text="Session" runat="server" OnClick="btn3_Click" />
<a href="ApplicationAndSession.aspx" >into ApplicationAndSession</a>
protected void btn2_Click(object sender, EventArgs e)ApplicationAndSession.aspx
{
Application["name"] = this.btn2.Text;
Server.Transfer("ApplicationAndSession.aspx");//ApplicationAndSession.aspx?name="1234"
}
protected void btn3_Click(object sender, EventArgs e)
{
Session["pwd"] = this.btn3.Text;
}
<div>ApplicationAndSession.aspx.cs :session["pwd"],session对象需要判空处理
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
</div>
if (!IsPostBack)
{
this.txtname.Text = Application["name"].ToString();
//this.txtpwd.Text = string.IsNullOrEmpty(Session["pwd"].ToString())?"is null":Session["pwd"].ToString();
if (Session["pwd"] != null)
{
this.txtpwd.Text = Session["pwd"].ToString();
}
}