Ishan
Ishan

Reputation: 4028

session,cookies in asp.net c#

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#

Upvotes: 1

Views: 2657

Answers (3)

yonan2236
yonan2236

Reputation: 13649

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

Upvotes: 1

Rahul Soni
Rahul Soni

Reputation: 4968

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

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

Upvotes: 0

lahsrah
lahsrah

Reputation: 9173

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.

Upvotes: 1

Related Questions