- « This page contains content or formatting that is not valid. You can find more information in the affected sections
Pass page values from one to another page in ASP .NET
To Pass Values Between ASP.NET Web Pages
To Transfer page values to another page
we can use following:-
1. Response.Redirect (Use a query string.)
3. Session Variables
4. Application Variables
5. HttpContext by using Server.Transfer
We always come into situations in which we need to transfer values from one page to another page. In this article, I will show you some ways of transferring values from page to page. The page I created in this example is really simple which consists of a text field and a few buttons. The data entered in the text field will be transferred to another page by using different methods labeled on the buttons.
Using the code
Let’s first see how to transfer using Response.Redirect method. This maybe the easiest of them all. You start by writing some data in the text field, and when you finish writing the data, you press the button labeled ‘Reponse.Redirect’. One tip that I would like to share with you is, sometimes we want to transfer to another page inside the catch exception, meaning exception is caught and we want to transfer to another page. If you try to do this, it may give you a System.Threading exception. This exception is raised because you are transferring to another page leaving behind the thread running. You can solve this problem using:
This tells the compiler to go to page “WebForm5.aspx”, and “false” here means that don’t end what you were doing on the current page. You should also look at the System.Threading class for threading issues. Below, you can see the C# code of the button event. “txtName” is the name of the text field whose value is being transferred to a page called “WebForm5.aspx”. “Name” which is just after “?” sign is just a temporary response variable which will hold the value of the text box.
Okay, up till this point, you have send the values using Response. But now, where do I collect the values, so in the “WebForm5.aspx” page_Load event, write this code. First, we check that the value entered is not null. If it’s not, then we simply display the value on the page using a Label control. Note: When you use Response.Redirect method to pass the values, all the values are visible in the URL of the browser. You should never pass credit card numbers and confidential information via Response.Redirect.
Next up is cookies. Cookies are created on the server side but saved on the client side. In the button click event of ‘Cookies’, write this code:
First, we create a cookie named “cName”. Since one cookie instance can hold many values, we tell the compiler that this cookie will hold “Name” value. We assign to it the value of the TextBox and finally add it in the Response stream, and sent it to the other page using Response.Redirect method.
Let’s see here how we can get the value of the cookie which is sent by one page.
As you see, it’s exactly the same way as we did before, but now we are using Request.Cookies instead of Request.QueryString. Remember that some browsers don’t accept cookies.
Next we see the session variables which are handled by the server. Sessions are created as soon as the first response is being sent from the client to the server, and session ends when the user closes his browser window or some abnormal operation takes place. Here is how you can use session variables for transferring values. Below you can see a Session is created for the user and “Name” is the key, also known as the Session key, which is assigned the TextBox value.
Sometimes, we need to access a value from anywhere in our page. For that, you can use Application variables. Here is a small code that shows how to do that. Once you created and assigned the Application variable, you can retrieve its value anywhere in your application.
You can also use HttpContext to retrieve values from pages. The values are retrieved using properties or methods. It’s a good idea to use properties since they are easier to code and modify. In your first page, make a property that returns the value of the TextBox.
We will use Server.Transfer to send the control to a new page. Note that Server.Transfer only transfers the control to the new page and does not redirect the browser to it, which means you will see the address of the old page in your URL. Simply add the following line of code in ‘Server.Transfer’ button click event:
Now, let’s go to the page where the values are being transferred, which in this case is “webForm5.aspx”.
please refer actual article at
Thanks & Regards,
March 13, 2012
В· Infoyen В· No Comments
Tags: how to transfer data one page to another page, How to: Pass Values Between ASP.NET Web Pages, Send Data from one page to another page in ASP.NET, Transferring page values to another page В· Posted in: ASP .NET