Transfer Data dari Page 1 ke Page 2 Di ASP.NET Webform

Transfer Data dari Page 1 ke Page 2 Di ASP.NET Webform

In most of the present day Web Applications, it is required to transfer data from one web page to another web page. Requirements like maintaining user preferences across the application, identifying user between transactions, correlating data based on previous page options etc, always drive programmers to write code to transfer data from page to page.

There are numerous ways in accomplishing the above said tasks, selecting an appropriate way for an appropriate situation is vested in the hands of programmer. Some of the most prominent techniques, out of my knowledge are listed in this article.

Data Transfer Techniques:
  1. QueryString
  2. PreviousPage.FindControl() and Request.Form[]
  3. Session State
  4. Cookies
  5. Application Variables
  6. Context.Items[]

Page A —> Transfer Data —> Page B

1) Using QueryString:
  • A query string is information that is appended to the end of a page URL.
  • Query strings provide a simple but limited way to maintain state information.
  • Do not rely on query strings to convey important or sensitive data, because data can be tampered by a user at any point. And also a user can bookmark the URL and thereby the information in it.
Page A:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("B.aspx?nameA="+TextBox1.Text);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Page B:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string nameB = Request.QueryString["nameA"].ToString();
        Response.Write(nameB);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>
2) Using PreviousPage.FindControl():
  • This technique is much more helpful when it comes to Cross Page postback previous page data access.
  • When we use Tranfer method or cross page postback, this FindControl() method is helpful in getting the PreviousPage data, but if we request a page directly, then PreviousPage renders null reference.
  • Even Request.Form[“Control ID”] can also be used to retrieve previous page data when transfer method is used (especially when the two pages are in different applications).
  • When we want to find control inside a template, then we need to refer the container containing the control at first and then we should find control.
  • Using with Master pages, first we need to find the content place holder in the master page and then we got to find the actual control in the content place holder (Ex: ContentPlaceHolder cph = (ContentPlaceHolder)Page.PreviousPage. FindControl("contentplaceholder1");).
Page A:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            TextBox tb = (TextBox)Page.PreviousPage.FindControl("TextBox1");
            if (tb != null)
            {
                Response.Write(tb.Text);
            }
        }        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>
3) Using Session State:
  • This is a server side state management technique. It main advantages are Data persistence, simple implementation, platform scalability, cookieless support.
  • The main drawback of this method is it reduces the application performance. The more the data in the session and more number of sessions can degrade the application performance by consuming more server memory. This can be avoided by removing session variables from time to time using Session.Remove(“variable name”).
Page A:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["nameA"] = TextBox1.Text;
        Response.Redirect("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Page B:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["nameA"] != null)
        {
            Response.Write(Session["nameA"].ToString());
        }               
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>
4) Using Cookies:
  • The main advantages of transferring data using cookies are server resources are not required, data persistence, simplicity and flexible configuration expiration rules.
  • The disadvantages of cookies are its size limitations, high chance of getting tampered at client side.
Page A:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie Cookie = new HttpCookie("nameA");
        Cookie.Value = TextBox1.Text;
        Cookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(Cookie);
        Response.Redirect("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["nameA"] != null)
        {
            Response.Write(Request.Cookies["nameA"].Value.ToString());
        }               
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>
5) Using Application Variables:
  • The main advantage of using application variables to transfer data is the data available throughout the application. And also these are simple to use.
  • The disadvantages of application variables are consumption of resources, limited durability and even global scope is also a drawback in certain cases.
Page A:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application["nameA"] = TextBox1.Text;
        Response.Redirect("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["nameA"] != null)
        {
            string nameB = Application["nameA"].ToString();
            Response.Write(nameB);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>
6) Using Context:
  • The Context.Items collection is also capable of storing objects instead of just plain character data.
  • Context.Items only last for current request and once the page postbacks the data in it will be lost, this is a required feature in many applications.
Page A:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Context.Items.Add("nameA",TextBox1.Text);
        Server.Transfer("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
            string nameB = Context.Items["nameA"].ToString();
            Response.Write(nameB);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>
Conclusion:
Different ways to pass data between two web pages are discussed. Practical implementation with Response.Redirect() and Server.Transfer() has been done. The advantages and disadvantages of all the practical methods are discussed.

0 KOMENTAR