RSS

Passing Values between ASP.NET Web Forms


Many times you will come across a scenario where you will have to pass some values to the next form that you are calling. There are many such approaches of passign values in ASP.NET
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 :

  1. The most easiest and the simplest of the methods.



  1. Doesnt have overload on the server


Cons

  1. Values turn up in the URL in address bar, hence user can see the values being passed



  1. User can manipulate the values in the URL



  1. 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 :

  1. 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.



  1. Since it will be available across all pages saves us the trouble for passing ti on every page.


Cons

  1. Data stored at server side



  1. Explicitly need to remove the data from session once your work is done with the data.



  1. 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.



  1. 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;
}
}

Post a Comment

Copyright © Shounak S. Pandit. Powered by Blogger.

Ads