1

i want to pass a string value from one page to another.Also i have some text boxes and the values entered in it needs to be passed to a new page.How do i do it?

I have a string S

String S = Editor1.Content.ToString();

i want to pass value in string S onto a new page i.e Default2.aspx how can i do this in ASP.net C#

3 Answers 3

1

You can achieve it using Session or by QueryString

By Session
In your first page:

String S = Editor1.Content.ToString();
Session["Editor"] = S;

Then in your next page access the session using:

protected void Page_Load(object sender, EventArgs e)
{
    String editor = String.Empty;
    if(!String.IsNullOrEmpty(Session["Editor"].ToString()))
    {
        editor = Session["Editor"].ToString();
        // do Something();
    }
    else
    {
        // do Something();
    }
}

-

By QueryString
In your first page:

// or other events
private void button1_Click(object sender, EventArgs e)
{
    String S = Editor1.Content.ToString();
    Response.Redirect("SecondPage.aspx?editor" + S)
}

In your second page:

protected void Page_Load(object sender, EventArgs e)
{
    string editor = Request.QueryString["editor"].ToString();
    // do Something();
}
Sign up to request clarification or add additional context in comments.

2 Comments

i used sessions but when i tried to print string editor i got the error as Use of unassigned local variable 'editor'
what did you use? Session or querystring? at your second page?
1

Depends on what the value is. If it is just a parameter and is ok to be viewed by the user then it can be passed through QueryString.

e.g.

Response.Redirect("Default2.aspx?s=value")

And then accessed from the Default2 page like

string s = Request.QueryString["s"];

If it needs to be more secure then consider using session, but I wouldn't recommend using the Session excessively as it can have issues, especially if you are storing the session InProc which is ASP.NET default.

You can have a state server or database but, it might be better to have your own database based session based on the authenticated user, and have it cached in the website if need be.

Comments

0

Use Session["content"]=Editor1.Content.ToString() in page1...

in page2 use...string s = Session["content"]

2 Comments

Than You.Im gettin the following error Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
@user243680 change string s = Session["content"] to string s = Session["content"] as string;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.