HtmlSelect.ClearSelection Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Clears the list selection of the HtmlSelect control and sets the ListItem.Selected property of all items to false.

Namespace:   System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

protected virtual void ClearSelection()

The ClearSelection method iterates through the ListItemCollection collection represented by the HtmlSelect control's Items property and sets the Selected property of each ListItem object to false.

The following code example demonstrates how to override the ClearSelection method in a custom server control so that it always clears all selected items of an HtmlSelect collection.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
        <title>Custom HtmlSelect - ClearSelection - C# Example</title>
        <script runat="server">
      void HtmlInputButton1_ServerClick(Object sender, EventArgs e) 
        if (HtmlSelect1.SelectedIndex >= 0)
          if (HtmlSelect1.Multiple == true)
            Div1.InnerHtml = "You selected:";
            for (int i=0; i<=HtmlSelect1.Items.Count - 1; i++)
              if (HtmlSelect1.Items[i].Selected == true)
                Div1.InnerHtml += "<br /> &nbsp;&nbsp; " + HtmlSelect1.Items[i].Value; 
            Div1.InnerHtml = "You selected " + HtmlSelect1.Value;
          Div1.InnerHtml = "You did not select an item.";
        <form id="Form1" method="post" runat="server">
            <h3>Custom HtmlSelect - ClearSelection - C# Example</h3>

        id="HtmlSelect1" multiple
        <option value="Option1">Option1</option>
        <option value="Option2">Option2</option>
        <option value="Option3">Option3</option>
        <option value="Option4">Option4</option>

        name="HtmlInputButton1" />&nbsp;&nbsp;

      <br />
      <div id="Div1" runat="server" 
        style="DISPLAY: inline; WIDTH: 256px; HEIGHT: 15px" />

using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomHtmlSelectClearSelection : System.Web.UI.HtmlControls.HtmlSelect
        protected override void ClearSelection()
            // For each item in the Items collection, 
            // set the Selected property to false.
            for (int i=0; i < Items.Count; i++)
            Items[i].Selected = false;

.NET Framework
Available since 1.1
Return to top