ASP.NET - pass a value stored in a string to another page

Asked By Neethu on 14-Nov-11 06:20 AM
hi all

how to pass  a  string value to another form without using session & querystring.
Kirtan Patel replied to Neethu on 14-Nov-11 06:23 AM
you can pass the data using 3 things ..
 
1. using Session
 
you can use Session to Store values in it
 
 Session["Name"] = SomeValue
 
 
and Retrieve it at another page using
 
TextBox1.Text = Session["Name"].ToString();
 
(Casting is must )
 
2. using QueryString
 
you can pass data by using QueryString when nevigating to nextpage
 
 Response.Redirect("Page2.aspx?Name=" + SomeValue + "&Age=" + SomeValue2);
 
 
3.using Class
 
you can define public static  instance variavles in class and set its value when needed and retrive those values back at page2 with code like
 
 TextBox1.Text = Class1.name;
 
 
Thank you :)
 
 
Jitendra Faye replied to Neethu on 14-Nov-11 06:30 AM
For that use Server.Transer().

Source Web Form
Add following properties to the web form:
public string Name
{
	get
	{
		return TextBox1.Text;
	}
}

public string EMail
{
	get
	{
		return TextBox2.Text;
	}
}

Now, call Server.Transfer.
private void Button1_Click
(object sender, System.EventArgs e)
{
	Server.Transfer("anotherwebform.aspx");
}
Destination Web Form
private void Page_Load
(object sender, System.EventArgs e)
{
	//create instance of source web form
	WebForm1 wf1;
	//get reference to current handler instance
	wf1=(WebForm1)Context.Handler;
	Label1.Text=wf1.Name;
	Label2.Text=wf1.EMail;
}
Chintan Vaghela replied to Neethu on 14-Nov-11 06:34 AM

In Defaul.aspx page call this,

Response.Redirect("SecondPage.aspx?id=1");

 

Fetch this value into SecondPage.aspx Page_Load Event to Retrive this value

        if (Request.QueryString["id"] != null)

        {

          lblHyperLink.Text = Request.QueryString["id"].ToString();

        }

dipa ahuja replied to Neethu on 14-Nov-11 06:35 AM
Without queryString and Session , you can pass it by class

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

 protected void Button1_Click(object sender, EventArgs e)
  {
    Response.Redirect("Default.aspx");
  }

On Default.aspx:

protected void Page_Load(object sender, EventArgs e)
  {
    string name = ((TextBox)this.PreviousPage.FindControl("TextBox1")).Text.ToString();
    Response.Write(name);
  }



Riley K replied to Neethu on 14-Nov-11 06:39 AM

Use Server.Transfer Method to pass values

Context.Items["UserName"] = TextBox1.Text; //Page1

Server.Transfer("Page2.aspx"); //Page1

string s = Context.Items["UserName"].ToString(); //Page2



Regards
Chintan Vaghela replied to Neethu on 14-Nov-11 06:44 AM

protected void Page_Load(object sender, EventArgs e)

    {

      // Find the server name on the previous page

      TextBox txt =

          (TextBox)Page.PreviousPage.FindControl("serverNameText");

      if (txt != null)

        prevServerName.Text = Server.HtmlEncode(txt.Text);

      else

        prevServerName.Text = "[Name Not available]";

    }


Reena Jain replied to Neethu on 14-Nov-11 08:42 AM
hi,

For passing variables content between pages ASP.NET gives us several choices. One choice is using QueryString property of Request Object. When surfing internet you should have seen weird internet address such as one below.

http://www.localhost.com/Webform2.aspx?name=Reena&lastName=Jain

This html addresses use QueryString property to pass values between pages. In this address you send 3 information.

   1. Webform2.aspx this is the page your browser will go.
   2. name=Atilla you send a name variable which is set to Atilla
   3. lastName=Ozgur you send a lastName variable which is set to Ozgur

As you have guessed ? starts your QueryString, and & is used between variables. Building such a query string in Asp.Net is very easy. Our first form will have 2 textboxes and one submit button.

Put this code to your submit button event handler.
private void btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Redirect("Webform2.aspx?Name=" this.txtName.Text + "&LastName=" +
this.txtLastName.Text);
}

Our first code part builds a query string for your application and send contents of your textboxes to second page. Now how to retrieve this values from second page. Put this code to second page page_load.

private void Page_Load(object sender, System.EventArgs e)
{
this.txtBox1.Text = Request.QueryString["Name"];
this.txtBox2.Text = Request.QueryString["LastName"];
}