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




0 comments

Posted in

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

"The View State is invalid for this page and might be corrupted " Server.Transfer with preserveForm attribute = true


Invalid viewstate viewstate corrupted ?
Ever got this error and thought what you did to deserve this ? since nowhere have you played with the viewstate of the page...


while all you did was use the statement : Server.Trasnfer(PageURL, true);


well here is the answer


Thats b'coz the viewstate is invalid in the second page and how so ? well lets start with what does Server.Trasfer() with preserveForm attribute set to true does.


The statement is going to transfer to the new page and keep the form members intact so that you can access it from the next page.


Which in turns means every control on the form will be accessible from the second form and since viewstate of a page is stored in a encrypted format in a hidden textbox on the form it will also

be accessible and present in the second form.



Now here lies the problem.

The second form will contain its own viewstate and now since we have used preserve form = true the viewstate of the previous form will also be present in the second form which is a
complete nono since how can one page have 2 viewstates.This is the reason why we get the above error.






Note : If you are running your application on a web farm then the error might also be because the validation key used for validating the viewstate is different for each
server in the web farm.




0 comments

Posted in

"The View State is invalid for this page and might be corrupted " Server.Transfer with preserveForm attribute = true


Invalid viewstate viewstate corrupted ?
Ever got this error and thought what you did to deserve this ? since nowhere have you played with the viewstate of the page...

while all you did was use the statement : Server.Trasnfer(PageURL, true);

well here is the answer

Thats b'coz the viewstate is invalid in the second page and how so ? well lets start with what does Server.Trasfer() with preserveForm attribute set to true does.

The statement is going to transfer to the new page and keep the form members intact so that you can access it from the next page.

Which in turns means every control on the form will be accessible from the second form and since viewstate of a page is stored in a encrypted format in a hidden textbox on the form it will also

be accessible and present in the second form.

Now here lies the problem.

The second form will contain its own viewstate and now since we have used preserve form = true the viewstate of the previous form will also be present in the second form which is a
complete nono since how can one page have 2 viewstates.This is the reason why we get the above error.

Note : If you are running your application on a web farm then the error might also be because the validation key used for validating the viewstate is different for each
server in the web farm.

Surviving


posting something to prove my existence in this blogosphere :)

The project i am working on is a great learning thing for me working on reflection
Well to give a brief idea i have to create a utility which will compare 2 versions of a set of dlls and find the version compatibility issues arising out of it and make a report from the compatibility issues.
This is done by going through the members of a dll and populating a object model.
and later on comparing both the object models version 2.X and version 3.X to find the compatibility issues.

To do that have to use
  • Reflection for reading .Net assemblies

  • Tlbinf32.dll for reading COM files


Reflection was cool with loads of example to refer to on net and also the great MSDN
but the typle lib information dll tlibinf32.dll took away my sleep since
  • Its no longer supported by MS

  • The help file that i found referred to some code samples which were written for the previous release of tlbinf32.dll
just my luck...

so it was all RnD for the tlbinf32.dll for me. but finally got it done
will publish a post soon with some basic examples of tlbinf32.dll over here so that others dont have to go through the ordeal i went through.

ciao for now

Shaunak P
0 comments

Posted in

Surviving


posting something to prove my existence in this blogosphere :)

The project i am working on is a great learning thing for me working on reflection
Well to give a brief idea i have to create a utility which will compare 2 versions of a set of dlls and find the version compatibility issues arising out of it and make a report from the compatibility issues.
This is done by going through the members of a dll and populating a object model.
and later on comparing both the object models version 2.X and version 3.X to find the compatibility issues.

To do that have to use

  • Reflection for reading .Net assemblies



  • Tlbinf32.dll for reading COM files


Reflection was cool with loads of example to refer to on net and also the great MSDN
but the typle lib information dll tlibinf32.dll took away my sleep since

  • Its no longer supported by MS



  • The help file that i found referred to some code samples which were written for the previous release of tlbinf32.dll


just my luck...

so it was all RnD for the tlbinf32.dll for me. but finally got it done
will publish a post soon with some basic examples of tlbinf32.dll over here so that others dont have to go through the ordeal i went through.

ciao for now

Shaunak P
Copyright © Shounak S. Pandit. Powered by Blogger.

Ads