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

10 篇文章 0 订阅
9 篇文章 0 订阅

细说页面传值的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)
    {
        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();

            }

        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值