Passing Values between ASP.NET Web Forms
I will be discussing some of them over here.
# 1 Using Querystring
e.g
Calling form :
private void LoadForm2_Click(object sender, System.EventArgs e)
{
string value1 = "5002";
string value2 = "Shaunak Pandit";
string webForm2url = String.Empty;
webForm2url = "webForm2.aspx?empId=" + value1 + "&empName=" + value2;
Response.Redirect(webForm2url);
}
Destination form :
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["empName"];
Label2.Text=Request.QueryString["empId"];
}
Pros :
- The most easiest and the simplest of the methods.
- Doesnt have overload on the server
Cons
- Values turn up in the URL in address bar, hence user can see the values being passed
- User can manipulate the values in the URL
- URL becomes messy (not that we care though)
# 2 Using Session variables
Calling form :
private void Button2_Click(object sender, System.EventArgs e)
{
string value1 = "5002";
string value2 = "Shaunak Pandit";
Session["empId"] = value1;
Session["empName"] = "Shounak Pandit";
Response.Redirect("webform2.aspx");
}
Destination form :
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Label1.Text = Session["empName"].ToString();
Label2.Text = Session["empId"].ToString();
string df = Session["empgfdfdfId"].ToString(); <---- A
}
catch(Exception ex)
{
//.....///
}
}
Pros :
- Data saved in session and will remain there till the session is closed or till its explicitly, hence no need to pass it from one page to another once loaded in session.
- Since it will be available across all pages saves us the trouble for passing ti on every page.
Cons
- Data stored at server side
- Explicitly need to remove the data from session once your work is done with the data.
- Overload on server. e.g consider a case where there are more than thousands of users accesing your site simultaneously (remeber this is just an example) and you store some data in session.Then there will be million sessions saved in your server and each of these session will have the data i.e in turn million data.
- Overall a bad idea when you have multiple simulatneous users.
# 3 Using Server.Trasnfer and Context object
Calling form :
Add a property in the calling form
public string ID
{
get
{
return empIdTextbox.Text; // This need not be a control value it can also contain a member variable.
}
}
private void Context_Click(object sender, System.EventArgs e)
{
Server.Transfer("webform2.aspx");
}
Destination form :
private void Page_Load(object sender, System.EventArgs e)
{
try
{
//create instance of source web form.
WebForm1 formCallee;
//get reference to current handler instance.
formCallee =(WebForm1)Context.Handler;
//Call the property to retrieve the value.
Label1.Text=formCallee.ID;
}
catch(Exception ex)
{
string errMessage = ex.Message;
}
}
#4 Using Server.Trasnfer and preserve form attribute
Server.Transfer("webform2.aspx", true); <-- Note the second parameter to Server.Transfer is set to true this indicates that the form members are not to be discarded.
NOTE : There is a bug related to ASP.NET You can read stuff abt the bug at Server.Trasfer Bug post
Calling form :
Destination form :
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Label1.Text = Label1.Text = Request.Form["empIdTextbox"] ;
}
catch(Exception ex)
{
string errMessage = ex.Message;
}
}