| ||||||||||
IntroductionWe 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 codeResponse.RedirectLet's first see how to transfer 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 { // Value sent using HttpResponse Response.Redirect("WebForm5.aspx?Name="+txtName.Text); } Okay, up till this point, you have send the values using Label3.Text = Request.QueryString["Name"]; CookiesNext 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: HttpCookie cName = new HttpCookie("Name");cName.Value = txtName.Text; Response.Cookies.Add(cName); Response.Redirect("WebForm5.aspx"); First, we create a cookie named " Let's see here how we can get the value of the cookie which is sent by one page. if (Request.Cookies["Name"] != null )Label3.Text = Request.Cookies["Name"].Value; As you see, it's exactly the same way as we did before, but now we are using Session VariablesNext 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["Name"] = txtName.Text; Response.Redirect("WebForm5.aspx"); // The code below shows how to get the session value. // This code must be placed in other page. if(Session["Name"] != null) Label3.Text = Session["Name"].ToString(); Application VariablesSometimes, 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. // This sets the value of the Application VariableApplication["Name"] = txtName.Text; Response.Redirect("WebForm5.aspx"); // This is how we retrieve the value of the Application Variable if( Application["Name"] != null ) Label3.Text = Application["Name"].ToString(); HttpContextYou can also use { get { return txtName.Text; } } We will use Now, let's go to the page where the values are being transferred, which in this case is "webForm5.aspx". // You can declare this Globally or in any event you likeWebForm4 w; // Gets the Page.Context which is Associated with this page w = (WebForm4)Context.Handler; // Assign the Label control with the property "GetName" which returns string Label3.Text = w.GetName; Special NoteAs you see, there are various method in transferring values from one page to another. Each method has its own advantage and disadvantage. So, when you are transferring values, do your homework so that you have a better idea which approach is most feasible for you. |
'ASP.NET' 카테고리의 다른 글
클라이언트 MAC주소 알아오는 소스 (0) | 2005.08.25 |
---|---|
닷넷 공부에 꽤 큰 도움이 되는 사이트 (0) | 2005.08.23 |
숫자형식을 문자 형식으로 바꾸는 소스 (0) | 2005.06.23 |
ASP.NET 마스터하기: 사용자 지정 엔터티 클래스 소개 (0) | 2005.05.21 |
asx 리스트 만들기 | (0) | 2005.05.21 |