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

ASP.NET 2.0 Internals


Posting after a long long time have been really busy with somethings anyways here is a good article on ASP.NET 2.0 Internals

Good article on ASP.NET 2.0 Internals

Explains the details of ASP.NET 2.0
Happy Reading !!!!
0 comments

Posted in

ASP.NET 2.0 Internals


Posting after a long long time have been really busy with somethings anyways here is a good article on ASP.NET 2.0 Internals

Good article on ASP.NET 2.0 Internals

Explains the details of ASP.NET 2.0
Happy Reading !!!!

Visual Studio IDE : ShortCuts for Coding




Convert selected code to lower case - Ctrl+U
Convert selected code to upper case - Ctrl+Shift+U
Comment selected code - Ctrl+K, Ctrl+C
Uncomment selected code - Ctrl+K, Ctrl+U

WordWrap

Have a line of code which goes beyond the screen width? but you need to keep it that way and the only alternative is to scroll all the way to the right?
Well theres good news VS IDE provides a toggle shortcut which will put the line contents beyond the screen width on the next line and back onto the same line

ShortCut Key : - Ctrl + r + r (toggles)
0 comments

Posted in

Visual Studio IDE Tips & tricks: ShortCuts for Coding


Convert selected code to lower case - Ctrl+U
Convert selected code to upper case - Ctrl+Shift+U
Comment selected code - Ctrl+K, Ctrl+C
Uncomment selected code - Ctrl+K, Ctrl+U

WordWrap

Have a line of code which goes beyond the screen width? but you need to keep it that way and the only alternative is to scroll all the way to the right?
Well theres good news VS IDE provides a toggle shortcut which will put the line contents beyond the screen width on the next line and back onto the same line

ShortCut Key : - Ctrl + r + r (toggles)

Letting .Net remind you of the TODO task in your code


Letting .Net remind you of the TODO task in your code

.Net IDE provides a mechanism of keeping track of all the TODO tasks in your code.This is done through the Task window
Using the “TODO“ Keyword we can tell .NET IDE to remind us about this TODO task in the task window

HOWTO : - simply add a comment to your code that starts with the TODO keyword. It will automatically be added to your existing Task list.

E.g : - // TODO: Need to optimise the AuthoriseUser Method.

To view the Task list (ShortCut Key : Ctrl+Alt+K ):-

select View > Other Windows > Task List from the menu .
The Task list often filters its contents, so it displays only certain information. To view everything, right-click on your list and select All Tasks > All.


0 comments

Posted in

Visual Studio IDE Tips & tricks: Shortcut Letting .Net remind you of the TODO task in your code


Letting .Net remind you of the TODO task in your code

.Net IDE provides a mechanism of keeping track of all the TODO tasks in your code.This is done through the Task window
Using the “TODO“ Keyword we can tell .NET IDE to remind us about this TODO task in the task window

HOWTO : - simply add a comment to your code that starts with the TODO keyword. It will automatically be added to your existing Task list.

E.g : - // TODO: Need to optimise the AuthoriseUser Method.

To view the Task list (ShortCut Key : Ctrl+Alt+K ):-

select View > Other Windows > Task List from the menu .
The Task list often filters its contents, so it displays only certain information. To view everything, right-click on your list and select All Tasks > All.

Adding frequently used code to toolbar


Storing Often-Used Code in the Toolbox

If there is some often-used code and templates that you repeatedly have to use across various code behinds what would you usually do ?
Copy that code snippet somewhere , say ... in notepad or something and everytime you need it you will simply copy it back into the code behind. Well thats the most obvious thing to do, but did you know that VS IDE allows us to store code snippets into the toolbar for later use??

HowTO : - Simply drag and drop your code straight onto one of the toolbox tabs, such as the General tab or create your own tab . When you need to use it again, simply drag and drop back into your code window.These code snippets will persist there across projects.Hence making our job easier.

0 comments

Posted in

Visual Studio IDE Tips & tricks: shortcuts to code snippets Adding frequently used code to toolbarVisual Studio IDE Tips & tricks:


Storing Often-Used Code in the Toolbox

If there is some often-used code and templates that you repeatedly have to use across various code behinds what would you usually do ?
Copy that code snippet somewhere , say ... in notepad or something and everytime you need it you will simply copy it back into the code behind. Well thats the most obvious thing to do, but did you know that VS IDE allows us to store code snippets into the toolbar for later use??

HowTO : - Simply drag and drop your code straight onto one of the toolbox tabs, such as the General tab or create your own tab . When you need to use it again, simply drag and drop back into your code window.These code snippets will persist there across projects.Hence making our job easier.

Tsunami Help / Tsunami Relief fund / Tsunami where to donate


Tsunami,



Always thought that India was safe from the affects of Tsunami was a shock to hear abt the Wave hitting the southern India and having such a major impact.

Hope many people donate money to tsunami relief funds however small it may be the point to remember here is that even a amount negligible to us might make a big difference to some of the affected people there.



In case you want to have a look at how it was here is a link to some of the Tsunami videos Tsunami videos






The following has been extracted from Tsunami Updates site




In Pune :



a] Money in the form of cheque / demand draft contributions is an URGENT need. The payment could be made favouring : 'Indian Red Cross Society' and dropped at Red Cross office, Atur Sangtani Red Cross House, M.G.Road, Pune 411 001 OR with Ms.Vijaya Moorthy at 3 Silver Classic Apartments, S.R.P.F.Road, Wanowrie, Pune 411 040. You can contact Pune phone nos. 2613 0031 / cell no.98220 04752 for contributions. Red Cross donations are exempted under Sec.80-G of the Income Tax Act. Please mention your name /address/phone no. on a slip of paper attached to your DD/cheque.



b] MAITRI, an NGO, working in the affected areas of Tamil Nadu is sending out food items like rice, dal, mineral water bottles and dry milk powder eg.Amulya (1 kg.packs) on 7th January. Contributions of this nature can be made. Please contact Ms. Vinita Tatke at mobile no.94225 21702 OR vinitat@vsnl.com.



c] Doctors / psychologists ( only ) who can speak MALAYALAM are required for the Red Cross team to be sent out to affected areas of Kerala for relief work. Contact Prof.R V Kulkarni at phone no. 2613 0331 / Red Cross office 2613 0031 / Vijaya Moorthy at vmoorthy@rediffmail.com.



The above phone nos. pertaining to Pune. Those calling up from other parts of the country need to prefix 020 for landlines and 0 for mobile nos.




For more details regarding the above visit Tsunami Updates site






Here are some links to various Relief funds

Times relief Fund : - IndiaTimes Registration



TimesFoundation info



Tsunami Information Updates + some international organisation where you can Donate



Indian Red Cross
>

Info on Prime Ministers Fund The best fund to donate money is the Prime Ministers fund



Some info on where to donate on rediff









Shaunak Pandit

0 comments

Posted in

Tsunami Help / Tsunami Relief fund / Tsunami where to donate


Tsunami,


Always thought that India was safe from the affects of Tsunami was a shock to hear abt the Wave hitting the southern India and having such a major impact.

Hope many people donate money to tsunami relief funds however small it may be the point to remember here is that even a amount negligible to us might make a big difference to some of the affected people there.


In case you want to have a look at how it was here is a link to some of the Tsunami videos Tsunami videos





The following has been extracted from Tsunami Updates site




In Pune :


a] Money in the form of cheque / demand draft contributions is an URGENT need. The payment could be made favouring : 'Indian Red Cross Society' and dropped at Red Cross office, Atur Sangtani Red Cross House, M.G.Road, Pune 411 001 OR with Ms.Vijaya Moorthy at 3 Silver Classic Apartments, S.R.P.F.Road, Wanowrie, Pune 411 040. You can contact Pune phone nos. 2613 0031 / cell no.98220 04752 for contributions. Red Cross donations are exempted under Sec.80-G of the Income Tax Act. Please mention your name /address/phone no. on a slip of paper attached to your DD/cheque.


b] MAITRI, an NGO, working in the affected areas of Tamil Nadu is sending out food items like rice, dal, mineral water bottles and dry milk powder eg.Amulya (1 kg.packs) on 7th January. Contributions of this nature can be made. Please contact Ms. Vinita Tatke at mobile no.94225 21702 OR vinitat@vsnl.com.


c] Doctors / psychologists ( only ) who can speak MALAYALAM are required for the Red Cross team to be sent out to affected areas of Kerala for relief work. Contact Prof.R V Kulkarni at phone no. 2613 0331 / Red Cross office 2613 0031 / Vijaya Moorthy at vmoorthy@rediffmail.com.


The above phone nos. pertaining to Pune. Those calling up from other parts of the country need to prefix 020 for landlines and 0 for mobile nos.




For more details regarding the above visit Tsunami Updates site





Here are some links to various Relief funds

Times relief Fund : - IndiaTimes Registration


TimesFoundation info


Tsunami Information Updates + some international organisation where you can Donate


Indian Red Cross </a

>

Info on Prime Ministers Fund The best fund to donate money is the Prime Ministers fund


Some info on where to donate on rediff





Shaunak Pandit

0 comments

Posted in

Copyright © Shounak S. Pandit. Powered by Blogger.

Ads