ASP.Net/Page/Controls On Page — различия между версиями

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

Версия 15:30, 26 мая 2010

Add asp control to page dynamically

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Not IsPostBack
               If Session.IsNewSession Then
                  Session("foo") = "foo"
                  Message.Text = "The current Session (SessionID: " & _
                     Session.SessionID & ") was created with this request."
               Else
                  Message.Text = "Click the button to abandon the current session."
                  Dim AbandonButton As New Button
                  AbandonButton.Text = "Abandon Session"
                  myForm.Controls.Add(AbandonButton)
               End If
            Else
               Session.Abandon()
               Message.Text = "Session abandoned."
            End If
         End Sub
      </script>
   </head>
<body>
   <form id="myForm" runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>



Add control by index

<%@ Page Language="vb" %>
<html>
   <head>
      <title>Controls collection example</title>
      <script runat="server">
         Sub Page_Load()
            Message.Text = "There are currently " & Controls.Count & _
               " controls on the page.<br/>"
            Dim Message2 As New Label
            Controls.AddAt(Controls.Count - 1, Message2)
            Message2.Text = "There are now " & Controls.Count & _
               " controls on the page."
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>



Find Control in a page

<%@ Page Language="vb" %>
<html>
   <head>
      <title>FindControl method example</title>
      <script runat="server">
         Sub Page_Load()
            Dim TheControl As Control = FindControl("Message")
            If Not TheControl Is Nothing Then
               Dim TheLabel As Label = CType(TheControl, Label)
               TheLabel.Text = "Found the label named Message!"
               TheLabel.BackColor = System.Drawing.Color.Blue
            End If
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>



Page.HasControls property

<%@ Page Language="vb" %>
<html>
   <head>
      <title>HasControls method example</title>
      <script runat="server">
         Sub Page_Load()
            If Page.HasControls = True Then
               Message.Text = "The page contains controls."
            Else
               Message.Text = "The page does not contain controls."
            End If 
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>