tags:

views:

1120

answers:

9

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?

+2  A: 

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.

Sergio
+1  A: 

Query string

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

THEN in 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

cgreeno
+11  A: 

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.

On page 1:

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

On page 2:

string book = (PreviousPage as Page1).SomeProperty;
Serhat Özgel
+1 concise answer with specific examples. Good job.
GregD
A: 

Another option is cross-page postback.

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

marcumka
A: 

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:

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

This will generate the following response: this_is_a_value_passed_to_this_page

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

Eytan Levit
A: 

Use QueryString parameters or Session variables.

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.

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
  }
}
Yaakov Ellis
A: 

If you only want the variable to last within that one call to another page, I advise using 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

Ian Devlin
A: 

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.

The short of it is:

On the first page (Default.aspx) create a hidden text box (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.

Dscoduc
A: 

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.

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.