简体   繁体   English

将多个变量传递给asp.net中的另一页

[英]passing more than one variable to another page in asp.net

I want to pass more than one variable to other web pages. 我想将多个变量传递给其他网页。

I try this code 我尝试这段代码

string adi = TaskGridView.SelectedRow.Cells[3].Text;

string soyadi = TaskGridView.SelectedRow.Cells[3].Text;

Response.Redirect("Default2.aspx?adi=" + adi);

Response.Redirect("Default2.aspx?soyadi=" + adi);

but it doesnt work how can I do? 但它不起作用怎么办?

The safest way is to use String.Format() with Server.UrlEncode() : 最安全的方法是将String.Format()Server.UrlEncode()

Response.Redirect(String.Format("Default2.aspx?adi={0}&soyadi={1}", Server.UrlEncode(adi), Server.UrlEncode(soyadi)));

This will ensure that the querystring values will not be broken if there is an ampersand ( & ) for example, in the value. 这将确保,如果有一个号(查询字符串值将不会被打破&例如),在该值。

Then on Default2.aspx you can access the values like: 然后可以在Default2.aspx上访问以下值:

Server.UrlDecode(Request.QueryString["adi"]);

Concatenate them like this: 像这样连接它们:

Response.Redirect("Default2.aspx?adi=" + adi + "&soyadi=" + soyadi);

When passing query string parameters, use the ? 传递查询字符串参数时,请使用? symbol just after the name of the page and if you want to add more than one parameter, use the & symbol to separate them 符号仅在页面名称后面,如果要添加多个参数,请使用&符号将其分开

In the consuming page: 在使用页面中:

    protected void Page_Load(object sender, EventArgs e)
    {
        var adi = this.Request.QueryString["adi"];
        var soyadi = this.Request.QueryString["soyadi"];
    }

You can also use the session to pass values from one page to another 您还可以使用会话将值从一页传递到另一页

Set the values in the page where you want to pass the values from in to a session 在您想要将值从中传递到会话的页面中设置值

Session["adi"] = TaskGridView.SelectedRow.Cells[3].Text;

Session["soyadi"] = TaskGridView.SelectedRow.Cells[3].Text;

In the Page where you want to retrieve- You do like this.. 在您要检索的页面中-您确实如此。

string adi=(string)(Session["adi"]);
string soyadi=(string)(Session["soyadi"]);

You can as well pass values through ViewState or Session the diffrent with what you doing now is: People will not see anything in your url and have no idea what happend in backend. 您也可以通过ViewStateSession传递值,这与您现在所做的不同:人们不会在您的url中看到任何内容,也不知道后端发生了什么。 It's good when you passing some "topsecret" data ;P 当您传递一些“绝密”数据时,这很好; P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM