Tuesday, March 9, 2010

Use of PostBackUrl in ASP.NET 2.0

MyPage.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>My Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <table cellpadding="0" cellspacing="0" border="2" height="50px" style="width: 33%">
        <tr>
          <td align="center" valign="top" style="padding-top:20px;">
            <asp:TextBox ID="TxtName" runat="server"></asp:TextBox><br/><br/>
            <asp:Button ID="BtnSubmit" runat="server" PostBackUrl="http://www.codeanswer.blogspot.com" Text="Submit" OnClick="Button1_Click" />
          </td>
        </tr>
      </table>
    </div>
  </form>
</body>
</html>

When you post to another page in ASP.NET 2.0, you can reach out to the values in the page using the following methods:

MyPage.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {}
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox txtName = (TextBox)
            PreviousPage.FindControl("txtName");
            this.TxtName.Text= txtName.Text;
        }
    }
}