ASP.NET&C#学习笔录6(页面传值)

时间:2023-01-12 08:16:51

细说页面传值的N种方式

1 一个页面可以有多个form表单,但是只能有一个runat="server"的form

2 页面传值的7种方式~七剑下天山喽

3 get 和 post的区别  (get请求时,通过url传给服务器的值。post请求时,通过表单发送给服务器的值)

超链接形式&Get形式 :

default.aspx:

ASP.NET&C#学习笔录6(页面传值)

default2.aspx.cs:

ASP.NET&C#学习笔录6(页面传值)

Post形式:post提交这里写了2种形式的,第一种通过js脚本提交form,第二种form提交。

default.aspx:

ASP.NET&C#学习笔录6(页面传值)

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)
{
if (Request.Form["txtName"] != null)
{
this.text.Text = Request.Form["txtName"];
}
}
Response.Redirect形式
defaultNew.aspx:

 <p>server控件点击事件响应一个跳转页面的事件</p>
<asp:Button ID="btn1" Text="server控件into Default3" runat="server" OnClick="btn1_Click" />
default.aspx.cs

 protected void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("Default3.aspx?name=" + btn1.Text.ToString());
}
default3.aspx

 <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>
<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>
default.aspx.cs:

 protected void btn2_Click(object sender, EventArgs e)
    {
        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;
    }
ApplicationAndSession.aspx

<div>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
</div>
ApplicationAndSession.aspx.cs :session["pwd"],session对象需要判空处理

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();

}

}