Tangara G

Tangara G

  • 1.7k
  • 298
  • 67.6k

What is wrong with my cross page post back ?

Dec 26 2016 4:48 AM
Hi,
 
I tried out this cross page post back to understand the concept but it doesn't give me anything on the 2nd page.  Hope someone can tell me what went wrong with my code. Tks.
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="hiddenfieldtest.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>Cross Page Postback Example in asp.net</title>  
  8. </head>  
  9. <body>  
  10. <form id="form1" runat="server">  
  11. <div>  
  12. <table>  
  13. <tr>  
  14. <td><b>Enter UserName:</b></td>  
  15. <td><asp:TextBox ID="txtUserName" runat="server"/></td>  
  16. </tr>  
  17. <tr>  
  18. <td><b>Enter Location:</b></td>  
  19. <td><asp:TextBox ID="txtLocation" runat="server"/></td>  
  20. </tr>  
  21. <tr>  
  22. <td></td>  
  23. <td><asp:Button ID="btnPostback" Text="Postback" runat="server" PostBackUrl="~/WebForm2.aspx" /> </td>  
  24. </tr>  
  25. </table>  
  26. </div>  
  27. </form>  
  28. </body>  
  29. </html> 
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="hiddenfieldtest.WebForm2" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.      <b><u>WebForm2.aspx Page</u></b><br /><br />  
  13. <label id="lblName" runat="server" /><br /><br />  
  14. <label id="lblLocation" runat="server" />  
  15.     </div>  
  16.     </form>  
  17. </body>  
  18. </html> 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace hiddenfieldtest  
  9. {  
  10.     public partial class WebForm2 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)  
  15.             {  
  16.                 TextBox txtName = (TextBox)PreviousPage.FindControl("txtUserName");  
  17.                 TextBox txtLocation = (TextBox)PreviousPage.FindControl("txtLocation");  
  18.                 lblName.InnerText = "Welcome to WebForm2.aspx page " + txtName.Text;  
  19.                 lblLocation.InnerText = "Your Location: " + txtLocation.Text;  
  20.             }  
  21.             else  
  22.             {  
  23.                 Response.Redirect("WebForm1.aspx");  
  24.             }  
  25.         }  
  26.     }  


Answers (9)