[ASP.NET]페이지간값전달,@Reference, Context.Handler, Server.Transfer

홈 > 공유팁! > 프로그램 관련
프로그램 관련

[ASP.NET]페이지간값전달,@Reference, Context.Handler, Server.Transfer

꽁스짱 0 1319

[ASP.NET]페이지간값전달,@Reference, Context.Handler, Server.Transfer


페이지간 데이터를 넘기기 위해 PostBackUrl, PreviousPage 속성을 이용해도 되지만 Context.Handler를 이용하여 넘기는 방법도 있다.

이를위해 첫번째 페이지에서 넘기고 싶은 텍스트박스와 같은 객체가 있다면 이를 리턴하는프로퍼티(Property)를 만들고 다음 페이지에서 Context.Handler로 첫번째 페이지 인스턴스를 받아와서 첫페이지에서 만들어 놓은 속성을 이용하여 텍스트박스와 같은 객체를 받아 값을 이용하는 것이다.

아래 예제를 보자.

1. firstPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="firstPage.aspx.cs" Inherits="WebApplication2.firstPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        이름 :
        <asp:TextBox ID="firstName" runat="server"></asp:TextBox>
        <br />
        나이 :
        <asp:TextBox ID="age" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    
    </div>
    </form>
</body>
</html>



2. firstPage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class firstPage : System.Web.UI.Page
    {
        public TextBox First  //속성정의
        {
            get
            {
                return firstName; //textBox자체를넘김
            }
        }
        public TextBox Age  //속성정의
        {
            get
            {
                return age; //textBox자체를넘김
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("secondPage.aspx");
        }
    }
}

3. secondPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="secondPage.aspx.cs" 
Inherits="WebApplication2.secondPage" %>
<%@ Reference Page="firstPage.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        이름과 나이 :
        <asp:TextBox ID="secondName" runat="server"></asp:TextBox>
    
    </div>
    </form>
</body>
</html>



4. secondPage.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class secondPage : System.Web.UI.Page
    {
        public firstPage fp;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
//첫페이지의 인스턴스(객체참조)를 얻는다.
                fp = (firstPage)Context.Handler;
                if (fp != null)
                {                  
                    secondName.Text = fp.First.Text;  //첫번째 페이지 First 속성 이용
                    secondName.Text += " " + fp.Age.Text;  
                }
            }
        }
    }
}
 
0 Comments
제목