ASP.NET Tutorial/Page Lifecycle/Cross Page Posting

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Cross-page post (VB.net)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = "Hello " & TextBox1.Text & "<br />" & "Date Selected: " & Calendar1.SelectedDate.ToShortDateString()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>First Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter your name:<br />
        <asp:Textbox ID="TextBox1" Runat="server"/>
        When do you want to fly?<br />
        <asp:Calendar ID="Calendar1" Runat="server"/>
        <br />
        <asp:Button ID="Button1" Runat="server" Text="Submit page to itself" 
         OnClick="Button1_Click" />
        <asp:Button ID="Button2" Runat="server" Text="Submit page to NextPage.aspx"
         PostBackUrl="NextPage.aspx" />
        <asp:Label ID="Label1" Runat="server"/>
    </form>
</body>
</html>

File: NextPage.aspx
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myTextbox1 As TextBox
        Dim myCalendar1 As Calendar
        
        myTextbox1 = CType(PreviousPage.FindControl("Textbox1"), TextBox)
        myCalendar1 = CType(PreviousPage.FindControl("Calendar1"), Calendar)
        
        Label1.Text = "Hello " & myTextbox1.Text & "<br />" & _
            "Date Selected: " & myCalendar1.SelectedDate.ToShortDateString()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>


Exposing the values of the control from a Property

index.aspx

<%@ 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">
    public TextBox pp_TextBox1
    {
        get
        {
            return TextBox1;
        }
    }
    public Calendar myCalendar1
    {
        get
        {
            return Calendar1;
        }
    }
    protected void Button1_Click (object sender, System.EventArgs e)
    {
        Label1.Text = "Hello " + TextBox1.Text + "<br />" +
            "Date Selected: " + Calendar1.SelectedDate.ToShortDateString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>First Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter your name:<br />
        <asp:Textbox ID="TextBox1" Runat="server">
        </asp:Textbox>
        When do you want to fly?<br />
        <asp:Calendar ID="Calendar1" Runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" Runat="server" Text="Submit page to itself" 
         OnClick="Button1_Click" />
        <asp:Button ID="Button2" Runat="server" Text="Submit page to NextPage.aspx"
         PostBackUrl="NextPage.aspx" />
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>
File: NextPage.aspx

<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="Default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">    
    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (Page.IsCrossPagePostBack) {
        Label1.Text = "Hello " + PreviousPage.myTextbox1.Text + "<br />" + 
           "Date Selected: " + 
           PreviousPage.myCalendar1.SelectedDate.ToShortDateString();
      }
      else
      {
         Response.Redirect("Default.aspx");
      }
    }    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>


New Cross-Page Posting

<%@ 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, System.EventArgs e)
    {
        Label1.Text = "Hello " + TextBox1.Text + "<br />" +
            "Date Selected: " + Calendar1.SelectedDate.ToShortDateString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>First Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter your name:<br />
        <asp:Textbox ID="TextBox1" Runat="server"/>
        When do you want to fly?<br />
        <asp:Calendar ID="Calendar1" Runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" 
                    Runat="server" 
                    Text="Submit page to itself" 
                    OnClick="Button1_Click" />
        <asp:Button ID="Button2" 
                    Runat="server" 
                    Text="Submit page to NextPage.aspx"
                    PostBackUrl="NextPage.aspx" />
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>
File: NextPage.aspx
<%@ 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, System.EventArgs e)
    {
        TextBox myTextbox1;
        Calendar myCalendar1;
        
        myTextbox1 = (TextBox)PreviousPage.FindControl("Textbox1");
        myCalendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
        
        Label1.Text = "Hello " + myTextbox1.Text + "<br />" + "Date Selected: " + myCalendar1.SelectedDate.ToShortDateString();
    }    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>


Page.IsCrossPagePostBack

index.aspx

<%@ 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">
    public TextBox pp_TextBox1
    {
        get
        {
            return TextBox1;
        }
    }
    public Calendar myCalendar1
    {
        get
        {
            return Calendar1;
        }
    }
    protected void Button1_Click (object sender, System.EventArgs e)
    {
        Label1.Text = "Hello " + TextBox1.Text + "<br />" +
            "Date Selected: " + Calendar1.SelectedDate.ToShortDateString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>First Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter your name:<br />
        <asp:Textbox ID="TextBox1" Runat="server">
        </asp:Textbox>
        When do you want to fly?<br />
        <asp:Calendar ID="Calendar1" Runat="server"></asp:Calendar>
        <br />
        <asp:Button ID="Button1" Runat="server" Text="Submit page to itself" 
         OnClick="Button1_Click" />
        <asp:Button ID="Button2" Runat="server" Text="Submit page to NextPage.aspx"
         PostBackUrl="NextPage.aspx" />
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>
File: NextPage.aspx

<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="Default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">    
    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (Page.IsCrossPagePostBack) {
        Label1.Text = "Hello " + PreviousPage.myTextbox1.Text + "<br />" + 
           "Date Selected: " + 
           PreviousPage.myCalendar1.SelectedDate.ToShortDateString();
      }
      else
      {
         Response.Redirect("Default.aspx");
      }
    }    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>


Page level property (VB.net)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
   Public ReadOnly Property pp_TextBox1() As TextBox
       Get
           Return TextBox1
       End Get
   End Property
    
   Public ReadOnly Property myCalendar1() As Calendar
       Get
           Return Calendar1
       End Get
   End Property
    
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Label1.Text = "Hello " & TextBox1.Text & "<br />" & _
           "Date Selected: " & Calendar1.SelectedDate.ToShortDateString()
   End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>First Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter your name:<br />
        <asp:Textbox ID="TextBox1" Runat="server">
        </asp:Textbox>
        When do you want to fly?<br />
        <asp:Calendar ID="Calendar1" Runat="server"/>
        <br />
        <asp:Button ID="Button1" Runat="server" Text="Submit page to itself" 
         OnClick="Button1_Click" />
        <asp:Button ID="Button2" Runat="server" Text="Submit page to NextPage.aspx"
         PostBackUrl="NextPage.aspx" />
        <asp:Label ID="Label1" Runat="server"/>
    </form>
</body>
</html>
File: NextPage.aspx
<%@ Page Language="VB" %>
<%@ PreviousPageType VirtualPath="Default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = "Hello " & PreviousPage.myTextbox1.Text & "<br />" & _
            "Date Selected: " & _
            PreviousPage.myCalendar1.SelectedDate.ToShortDateString()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Runat="server"></asp:Label>
    </form>
</body>
</html>


PreviousPageType directive

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
   Public ReadOnly Property pp_TextBox1() As TextBox
       Get
           Return TextBox1
       End Get
   End Property
    
   Public ReadOnly Property myCalendar1() As Calendar
       Get
           Return Calendar1
       End Get
   End Property
    
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Label1.Text = "Hello " & TextBox1.Text & "<br />" & _
           "Date Selected: " & Calendar1.SelectedDate.ToShortDateString()
   End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>First Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter your name:<br />
        <asp:Textbox ID="TextBox1" Runat="server"/>
        
        When do you want to fly?<br />
        <asp:Calendar ID="Calendar1" Runat="server"/>
        <br />
        <asp:Button ID="Button1" Runat="server" Text="Submit page to itself" 
         OnClick="Button1_Click" />
        <asp:Button ID="Button2" Runat="server" Text="Submit page to NextPage.aspx"
         PostBackUrl="NextPage.aspx" />
        <asp:Label ID="Label1" Runat="server"/>
    </form>
</body>
</html>
File: NextPage.aspx
<%@ Page Language="VB" %>
<%@ PreviousPageType VirtualPath="Default.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
      If Page.IsCrossPagePostBack Then
        Label1.Text = "Hello " & PreviousPage.myTextbox1.Text & "<br />" & _
            "Date Selected: " & _
            PreviousPage.myCalendar1.SelectedDate.ToShortDateString()
      Else
            Response.Redirect("Default.aspx")
      End If
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" Runat="server"/>
    </form>
</body>
</html>