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中声明的全局变量;

3.ViewState(页面级)使用方式: 作用域---页面级

保存数据方式:

复制代码代码如下:

ViewState["myKey"]="MyData"; 

读取数据方式:

String myData;

if(ViewState["myData"]!=null)

{

myData = (string)ViewState["myKey"]

扫描二维码关注公众号,回复: 5571350 查看本文章

}

 

ViewState不能存储所有的数据类型,仅支持:
String、Integer、Boolean、Array、ArrayList、Hashtable

 

不同页面之间的参数传递

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"];

4.通过cookie方法传递参数

HttpCookie keepCookie = new HttpCookie("Login"); //创建一个HttpCookie实例,Cookies名称为Login,实例只是一个容器,真正使用的是Cookie名称

keepCookie["userName"] = "www.kpdown.com"; //向Login中添加一个userName属性,并赋值

keepCookie.Expires = DateTime.Now.AddDays(2); //设定Cookies的有效期为两天

Response.Cookies.Add(keepCookie); //把Cookies对象返回给客户端

猜你喜欢

转载自www.cnblogs.com/ning123/p/10551258.html
今日推荐