Udostępnij za pośrednictwem


DropDownList.CreateControlCollection Metoda

Definicja

Tworzy kolekcję do przechowywania kontrolek podrzędnych.

protected:
 override System::Web::UI::ControlCollection ^ CreateControlCollection();
protected override System.Web.UI.ControlCollection CreateControlCollection ();
override this.CreateControlCollection : unit -> System.Web.UI.ControlCollection
Protected Overrides Function CreateControlCollection () As ControlCollection

Zwraca

ControlCollection

Zawsze zwraca wartość EmptyControlCollection.

Przykłady

W poniższym przykładzie kodu pokazano, jak zastąpić CreateControlCollection metodę w niestandardowej kontrolce serwera, aby zawsze zwracało nowe wystąpienie EmptyControlCollection klasy dla kontrolki DropDownList . Aby uzyskać informacje na temat używania niestandardowych kontrolek serwera, zobacz Przewodnik: tworzenie i używanie niestandardowej kontroli serwera sieci Web.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<!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>
    <title>Custom DropDownList - CreateControlCollection - C# Example</title>
    <script runat="server">
      private void Page_Load(object sender, System.EventArgs e)
      {
          DropDownList1.Items.Add(new ListItem("Item1", "Item1"));
          DropDownList1.Items.Add(new ListItem("Item2", "Item2"));
          DropDownList1.Items.Add(new ListItem("Item2", "Item2"));
      }
    </script>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom DropDownList - CreateControlCollection - C# Example</h3>

      <aspSample:CustomDropDownListCreateControlCollection
        id="DropDownList1"
        runat="server" />

    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<!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>
        <title>Custom DropDownList - CreateControlCollection - VB.NET Example</title>
        <script runat="server">
            Sub Page_Load(sender As Object, e As EventArgs)
                DropDownList1.Items.Add(New ListItem("Item1", "Item1"))
                DropDownList1.Items.Add(New ListItem("Item2", "Item2"))
                DropDownList1.Items.Add(New ListItem("Item2", "Item2"))
            End Sub
        </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom DropDownList - CreateControlCollection - VB.NET Example</h3>
            <aspSample:CustomDropDownListCreateControlCollection id="DropDownList1" runat="server" />
        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomDropDownListCreateControlCollection : System.Web.UI.WebControls.DropDownList
    {
        protected override System.Web.UI.ControlCollection CreateControlCollection()
        {
            // Return a new empty ControlCollection
            return new System.Web.UI.EmptyControlCollection(this);
        }
    }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomDropDownListCreateControlCollection
        Inherits System.Web.UI.WebControls.DropDownList

        Protected Overrides Function CreateControlCollection() As System.Web.UI.ControlCollection

            ' Return a new EmptyControlCollection
            Return New System.Web.UI.EmptyControlCollection(Me)
        End Function
    End Class
End Namespace

Dotyczy