如何将字符串从一个aspx.cs页面带到另一个页面?

时间:2022-03-25 07:12:23

I want to use a string that I have been using in one aspx.cs file over to another. I know this is easy, but how do I go about doing that?

我想使用我在一个aspx.cs文件中使用的字符串到另一个。我知道这很容易,但我该怎么做呢?

9 个解决方案

#1


You can do it in a query string. On your first page:

您可以在查询字符串中执行此操作。在您的第一页:

Response.Redirect("Second.aspx?book=codecomplete");

and on the second page

在第二页

string book = Request["book"];

This method will allow your users to see what you are passing to a second page. Alternatively you can place it in session object. To place it use:

此方法将允许您的用户查看您传递到第二页的内容。或者,您可以将其放在会话对象中。放置它使用:

Session["book"] = "codecomplete";

and to get it back use:

并让它回来使用:

string book = Session["book"] as string;

As a third alternative, you can use Server.Transfer. Use this method if you want to go to the second page on the server side. But notice that your user will continue to see the url of the first page on address bar.

作为第三种选择,您可以使用Server.Transfer。如果要转到服务器端的第二页,请使用此方法。但请注意,您的用户将继续在地址栏上看到第一页的网址。

On page 1:

在第1页:

this.SomeProperty = "codecomplete";
Server.Transfer("SecondPage.aspx");

On page 2:

在第2页:

string book = (PreviousPage as Page1).SomeProperty;

#2


You can either send it using a querystring or you can define a session variable to store it.

您可以使用查询字符串发送它,也可以定义会话变量来存储它。

The best option depends on what you want to use that string for.

最佳选择取决于您要使用该字符串的内容。

#3


Query string

Response.Redirect(page.aspx?val=whatever);

THEN in page.aspx

然后在page.aspx中

string myval = Request["whatever"]

OR

Server.Transfer("page.aspx", true);

Will perserve the form values from the first page if you want dont to make the switching of pages transparent

如果您不想使页面切换透明,将保留第一页的表单值

#4


Another option is cross-page postback.

另一种选择是跨页回发。

http://msdn.microsoft.com/en-us/library/ms178139.aspx

#5


I strongly urge you to use the session only if absolutely neccessery , it takes valuable server resources.

我强烈建议您仅在绝对必要的情况下使用会话,这需要宝贵的服务器资源。

Use the QueryString, it's really simple to use it, you just add a "?" after the aspx and write the value you want to pass for example:

使用QueryString,使用起来非常简单,只需添加一个“?”在aspx之后写下你要传递的值,例如:

page.aspx?val=this_is_a_value_passed_to_this_page.

When in page.aspx, you read querystring values like this: string val = Request.QueryString["val"]; Response.Write(val);

在page.aspx中,你读取这样的查询字符串值:string val = Request.QueryString [“val”];回复于(VAL);

This will generate the following response: this_is_a_value_passed_to_this_page

这将生成以下响应:this_is_a_value_passed_to_this_page

A more complicated explanation can be found here: http://aspnet.4guysfromrolla.com/articles/020205-1.aspx

可以在这里找到更复杂的解释:http://aspnet.4guysfromrolla.com/articles/020205-1.aspx

#6


Use QueryString parameters or Session variables.

使用QueryString参数或会话变量。

Keep in mind on the receiving page that when checking the Request or Session object, you should make sure that the value is not null before trying to use it to avoid unanticipated errors.

请记住在接收页面上,在检查Request或Session对象时,在尝试使用它来避免意外错误之前,应确保该值不为null。

if (Session["VariableName"] != null) {
  string varName = Session["VariableName"].ToString();
  // use varName
}

Or

string varName = (Session["VariableName"] ?? "").ToString();

Likewise, if you are passing a numeric value, make sure that it is the correct data type before casting or converting and using it.

同样,如果要传递数值,请在转换或转换并使用它之前确保它是正确的数据类型。

if (Session["IDValue"] != null) {
  string idVlaueString= Session["IDValue"].ToString();
  int idValue = 0;
  bool isInt = int.TryParse(idValueString, out idValue);
  if (isInt) {
    // use idValue
  }
}

#7


If you only want the variable to last within that one call to another page, I advise using Context.

如果您只希望变量在该调用中持续调用另一个页面,我建议使用Context。

In the first page use:

在第一页使用:

Context.Items.Add("varName", varData);

And then on the called page use:

然后在被调用的页面上使用:

Context.Items("varName")

Read this article for more information: http://steveorr.net/articles/PassData.aspx

阅读本文以获取更多信息:http://steveorr.net/articles/PassData.aspx

#8


Check out an article on Alliance titled "Passing Data the .NET way" that shows how to accomplish this. The best part of this solution is that it doesn't use any query string variables that the user can see, nor does it require you to place it in the session state.

查看一篇题为“以.NET方式传递数据”的联盟文章,该文章介绍了如何实现这一目标。此解决方案的最佳部分是它不使用用户可以看到的任何查询字符串变量,也不需要将其置于会话状态。

The short of it is:

缺点是:

On the first page (Default.aspx) create a hidden text box (txtMessage):

在第一页(Default.aspx)上创建一个隐藏文本框(txtMessage):

<asp:TextBox ID="txtMessage" runat="server" Visible="False" Text="" />

Then add a method in the code behind:

然后在后面的代码中添加一个方法:

Public ReadOnly Property MessageForwarded() As String
    Get
        Return txtMessage.Text
    End Get
End Property

Finally, you can dynamically set the value of the hidden textbox

最后,您可以动态设置隐藏文本框的值

txtMessage.text = "Hello there"

On the second page add a reference:

在第二页添加引用:

<%@ Reference Page="Default.aspx" %>

and then load the data from the first page using the syntax:

然后使用以下语法从第一页加载数据:

Dim objSource As Source = CType(Context.Handler, Source)
If Not (objSource Is Nothing) Then
    Response.Write(objSource.MessageForwarded)
End If

I've been using this for some time without any problems.

我已经使用了一段时间没有任何问题。

#9


The query string is the good method, for passing a string value. Check this solution and try it

查询字符串是传递字符串值的好方法。检查此解决方案并尝试

response.redirect("link to your redirect page.aspx?userName="+string+"");

Here userName is the name given to the string which you want to get in another page.

这里userName是您希望在另一个页面中获取的字符串的名称。

And the string is the one which you are passing to next page.

字符串是您传递到下一页的字符串。

Then get that string back by using

然后使用返回该字符串

string str = Request.QueryString["userName"];

this is the esay way of getting the string value.

这是获取字符串值的方法。

#1


You can do it in a query string. On your first page:

您可以在查询字符串中执行此操作。在您的第一页:

Response.Redirect("Second.aspx?book=codecomplete");

and on the second page

在第二页

string book = Request["book"];

This method will allow your users to see what you are passing to a second page. Alternatively you can place it in session object. To place it use:

此方法将允许您的用户查看您传递到第二页的内容。或者,您可以将其放在会话对象中。放置它使用:

Session["book"] = "codecomplete";

and to get it back use:

并让它回来使用:

string book = Session["book"] as string;

As a third alternative, you can use Server.Transfer. Use this method if you want to go to the second page on the server side. But notice that your user will continue to see the url of the first page on address bar.

作为第三种选择,您可以使用Server.Transfer。如果要转到服务器端的第二页,请使用此方法。但请注意,您的用户将继续在地址栏上看到第一页的网址。

On page 1:

在第1页:

this.SomeProperty = "codecomplete";
Server.Transfer("SecondPage.aspx");

On page 2:

在第2页:

string book = (PreviousPage as Page1).SomeProperty;

#2


You can either send it using a querystring or you can define a session variable to store it.

您可以使用查询字符串发送它,也可以定义会话变量来存储它。

The best option depends on what you want to use that string for.

最佳选择取决于您要使用该字符串的内容。

#3


Query string

Response.Redirect(page.aspx?val=whatever);

THEN in page.aspx

然后在page.aspx中

string myval = Request["whatever"]

OR

Server.Transfer("page.aspx", true);

Will perserve the form values from the first page if you want dont to make the switching of pages transparent

如果您不想使页面切换透明,将保留第一页的表单值

#4


Another option is cross-page postback.

另一种选择是跨页回发。

http://msdn.microsoft.com/en-us/library/ms178139.aspx

#5


I strongly urge you to use the session only if absolutely neccessery , it takes valuable server resources.

我强烈建议您仅在绝对必要的情况下使用会话,这需要宝贵的服务器资源。

Use the QueryString, it's really simple to use it, you just add a "?" after the aspx and write the value you want to pass for example:

使用QueryString,使用起来非常简单,只需添加一个“?”在aspx之后写下你要传递的值,例如:

page.aspx?val=this_is_a_value_passed_to_this_page.

When in page.aspx, you read querystring values like this: string val = Request.QueryString["val"]; Response.Write(val);

在page.aspx中,你读取这样的查询字符串值:string val = Request.QueryString [“val”];回复于(VAL);

This will generate the following response: this_is_a_value_passed_to_this_page

这将生成以下响应:this_is_a_value_passed_to_this_page

A more complicated explanation can be found here: http://aspnet.4guysfromrolla.com/articles/020205-1.aspx

可以在这里找到更复杂的解释:http://aspnet.4guysfromrolla.com/articles/020205-1.aspx

#6


Use QueryString parameters or Session variables.

使用QueryString参数或会话变量。

Keep in mind on the receiving page that when checking the Request or Session object, you should make sure that the value is not null before trying to use it to avoid unanticipated errors.

请记住在接收页面上,在检查Request或Session对象时,在尝试使用它来避免意外错误之前,应确保该值不为null。

if (Session["VariableName"] != null) {
  string varName = Session["VariableName"].ToString();
  // use varName
}

Or

string varName = (Session["VariableName"] ?? "").ToString();

Likewise, if you are passing a numeric value, make sure that it is the correct data type before casting or converting and using it.

同样,如果要传递数值,请在转换或转换并使用它之前确保它是正确的数据类型。

if (Session["IDValue"] != null) {
  string idVlaueString= Session["IDValue"].ToString();
  int idValue = 0;
  bool isInt = int.TryParse(idValueString, out idValue);
  if (isInt) {
    // use idValue
  }
}

#7


If you only want the variable to last within that one call to another page, I advise using Context.

如果您只希望变量在该调用中持续调用另一个页面,我建议使用Context。

In the first page use:

在第一页使用:

Context.Items.Add("varName", varData);

And then on the called page use:

然后在被调用的页面上使用:

Context.Items("varName")

Read this article for more information: http://steveorr.net/articles/PassData.aspx

阅读本文以获取更多信息:http://steveorr.net/articles/PassData.aspx

#8


Check out an article on Alliance titled "Passing Data the .NET way" that shows how to accomplish this. The best part of this solution is that it doesn't use any query string variables that the user can see, nor does it require you to place it in the session state.

查看一篇题为“以.NET方式传递数据”的联盟文章,该文章介绍了如何实现这一目标。此解决方案的最佳部分是它不使用用户可以看到的任何查询字符串变量,也不需要将其置于会话状态。

The short of it is:

缺点是:

On the first page (Default.aspx) create a hidden text box (txtMessage):

在第一页(Default.aspx)上创建一个隐藏文本框(txtMessage):

<asp:TextBox ID="txtMessage" runat="server" Visible="False" Text="" />

Then add a method in the code behind:

然后在后面的代码中添加一个方法:

Public ReadOnly Property MessageForwarded() As String
    Get
        Return txtMessage.Text
    End Get
End Property

Finally, you can dynamically set the value of the hidden textbox

最后,您可以动态设置隐藏文本框的值

txtMessage.text = "Hello there"

On the second page add a reference:

在第二页添加引用:

<%@ Reference Page="Default.aspx" %>

and then load the data from the first page using the syntax:

然后使用以下语法从第一页加载数据:

Dim objSource As Source = CType(Context.Handler, Source)
If Not (objSource Is Nothing) Then
    Response.Write(objSource.MessageForwarded)
End If

I've been using this for some time without any problems.

我已经使用了一段时间没有任何问题。

#9


The query string is the good method, for passing a string value. Check this solution and try it

查询字符串是传递字符串值的好方法。检查此解决方案并尝试

response.redirect("link to your redirect page.aspx?userName="+string+"");

Here userName is the name given to the string which you want to get in another page.

这里userName是您希望在另一个页面中获取的字符串的名称。

And the string is the one which you are passing to next page.

字符串是您传递到下一页的字符串。

Then get that string back by using

然后使用返回该字符串

string str = Request.QueryString["userName"];

this is the esay way of getting the string value.

这是获取字符串值的方法。