How to: Respond to User Clicks in BulletedList Web Server Controls

JavaScript Editor jscript editor     Web designer 



Main Page

You can configure a BulletedList control to display buttons for the control's items. When users click a button, the control posts the page back to the server and gives you an opportunity to run code in response to the user's click.

To respond to a user click in a BulletedList Web server control

  1. Create an event handler for the BulletedList control's Click event.

  2. In the event handler, get the Index property of the BulletedListEventArgs value passed to the handler.

  3. Using the index, get the appropriate item from the control and get the item's Text and Value properties.

Example

The following code example shows a page containing a BulletedList control that displays static items. The handler for the control's Click event displays which list item the user clicked.

Visual BasicВ CopyCode imageCopy Code
<%@ Page Language="VB"%>
<script runat="server">
    Protected Sub BulletedList1_Click(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.BulletedListEventArgs)
        Dim position As Integer = e.Index
        Dim li As ListItem = BulletedList3.Items(position)
        Label1.Text = "You selected = " & li.Text & _ 
             ", with value = " & li.Value
    End Sub
</script>
<html>
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <asp:BulletedList ID="BulletedList3" 
            runat="server" 
            DisplayMode="LinkButton" 
            OnClick="BulletedList1_Click">
            <asp:ListItem Value="A">Item 1</asp:ListItem>
            <asp:ListItem Value="B">Item 2</asp:ListItem>
            <asp:ListItem Value="C">Item 3</asp:ListItem>
        </asp:BulletedList>
        <br />
        <asp:Label ID="Label1" runat="server" text="" />
    </form>
</body>
</html>
C#В CopyCode imageCopy Code
<%@ Page Language="C#" %>
<script runat="server">
    protected void BulletedList1_Click(object sender, 
        BulletedListEventArgs e)
    {
        ListItem li = BulletedList1.Items[e.Index];
        Label1.Text = "You selected = " + li.Text + ", with value = " 
            + li.Value;
    }
</script>

<html>
<head id="Head1" runat="server">
</head>
<body>
    <form id="form1" runat="server">
        <asp:BulletedList ID="BulletedList1" 
            runat="server" 
            DisplayMode="LinkButton" 
            OnClick="BulletedList1_Click">
            <asp:ListItem Value="A">Item 1</asp:ListItem>
            <asp:ListItem Value="B">Item 2</asp:ListItem>
            <asp:ListItem Value="C">Item 3</asp:ListItem>
        </asp:BulletedList>
        <br />
        <asp:Label ID="Label1" runat="server" text="" />
    </form>
</body>
</html>

See Also



JavaScript Editor jscript editor     Web designer