Tuesday 4 December 2012

Cross-Paging in Asp.Net using c#


By default , button have postback  property. When you click the button it reloads the page itself. However  we can use the property PostBackUrl  to redirect to the another page.

If you want to use the data of one page to another page without using  session , object or anything else , you can just use cross-page in your project.

Cross page posting means you are posting a form data to another page. This is useful when you want to post data to another page and do not want incur the overhead of reloading the current page.

Below  code is given  with a simple example.
For this example we have to required two page. Below give page with design and coding. Just follow .
Cross-Page.aspx:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label>&nbsp;&nbsp; <asp:TextBox ID="TextBox1"
            runat="server"></asp:TextBox><br />
            <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox
                ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Log In" PostBackUrl="~/Cross_page2.aspx"/>
    </div>
    </form>
</body>
</html>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <b style="font-size: xx-large; color: #669900"> Welcome To AspMaterials Blog</b><br />
        <asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True"
            Font-Size="Large" ForeColor="Blue"></asp:Label>
    </div>
    </form>
</body>
</html>

Cross-Page2.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Cross_page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox tb = new TextBox();// this is a object for texbox
            tb=(TextBox)(PreviousPage.FindControl("TextBox1"));
            Label1.Text = tb.Text;
        }
    }
}

Run the code.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.