Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ListControl Constructor

Initializes a new instance of the ListControl class.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public ListControl()

The following code example demonstrates how to programmatically

create a ListBox control. The example uses a PlaceHolder control as a container for the ListBox.


<!-- The following example demonstrates how to programmatically 
create a ListBox. The example uses a PlaceHolder control as
a container for the ListBox. -->

<%@ Page Language="C#" AutoEventWireup="True" %>

<!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>ListBox Example</title>
<script runat="server">

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         // Create a new ListBox.
         ListBox listbox1 = new ListBox();

         // Create ListItems to populate the ListBox.
         ListItem listItem1 = new ListItem("Item 1","Value 1");
         ListItem listItem2 = new ListItem("Item 2","Value 2");

         // Add the ListItems to the ListBox.
         listbox1.Items.Add(listItem1);
         listbox1.Items.Add(listItem2);

         // Add the ListBox to the Controls collection of the PlaceHolder control.
         Place.Controls.Add(listbox1);
      }

   </script>

</head>
<body>

   <h3>ListBox Example</h3>
   <br />

   <form id="form1" runat="server">

      <asp:button id="Button1"
                  Text="Click to create a two item ListBox" 
                  OnClick="SubmitBtn_Click" 
                  runat="server" />
      <br /><br />
      <asp:PlaceHolder id="Place" runat="server"/>

   </form>

</body>
</html>
   


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.