This documentation is archived and is not being maintained.

HtmlSelect.ServerChange Event

Occurs when the selected items in the HtmlSelect control change between posts to the server.

[Visual Basic]
Public Event ServerChange As EventHandler
[C#]
public event EventHandler ServerChange;
[C++]
public: __event EventHandler* ServerChange;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type EventArgs.

Remarks

The ServerChange event is raised when the selected items in the HtmlSelect control change between posts to the server.

Note   The control must have viewstate enabled for the ServerChange event to work correctly.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to specify and code an event handler for the ServerChange event of the HtmlSelect control. The event handler determines whether the selected items are compatible with each other.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>

<head>

   <script runat="server">

      Sub Button_Click (sender As Object, e As EventArgs)
        
         Dim i As Integer

         Label1.Text = "You selected:"

         For i = 0 to Select1.Items.Count - 1
  
            If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br> &nbsp;&nbsp; -" & Select1.Items(i).Text
            End If         

         Next i

      End Sub

      Sub Server_Change (sender As Object, e As EventArgs)

         Dim i As Integer
         Dim Count As Integer = 0

         For i = 0 to Select1.Items.Count - 1
  
            If Select1.Items(i).Selected Then
               Count = Count + 1
            End If         

         Next i

         If Count > 1 And Select1.Items(0).Selected Then

            Label2.Text = "Hey! You can't select 'All' with another selection!!"
        
         Else

            Label2.Text = ""
    
         End If

      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list: <br><br>

      <select id="Select1" 
              Multiple="True"
              OnServerChange="Server_Change"
              runat="server">

         <option value="All"> All </option>
         <option value="1" Selected="True"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

      <br>

      <asp:Label id="Label2"
           runat="server"/>

   </form>

</body>

</html>

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

<html>

<head>

   <script runat="server">

      void Button_Click (Object sender, EventArgs e)
      {

         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; -" + Select1.Items[i].Text;      

         }

      }

      void Server_Change (Object sender, EventArgs e)
      {

         int Count = 0;

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Count++;      

         }

         if ((Count > 1) && (Select1.Items[0].Selected))

            Label2.Text = "Hey! You can't select 'All' with another selection!!";
        
         else

            Label2.Text = "";

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list: <br><br>

      <select id="Select1" 
              Multiple="True"
              OnServerChange="Server_Change"
              runat="server">

         <option value="All"> All </option>
         <option value="1" Selected="True"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

      <br>

      <asp:Label id="Label2"
           runat="server"/>

   </form>

</body>

</html>

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>

<head>

   <script runat="server">

      function Button_Click (sender : Object, e : EventArgs) : void
      {

         Label1.Text = "You selected:";

         for (var i :  int =0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; -" + Select1.Items[i].Text;      

         }

      }

      function Server_Change (sender : Object, e : EventArgs) : void
      {

         var Count : int = 0;

         for (var i : int =0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Count++;      

         }

         if ((Count > 1) && (Select1.Items[0].Selected))

            Label2.Text = "Hey! You can't select 'All' with another selection!!";
        
         else

            Label2.Text = "";

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list: <br><br>

      <select id="Select1" 
              Multiple="True"
              OnServerChange="Server_Change"
              runat="server">

         <option value="All"> All </option>
         <option value="1" Selected="True"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

      <br>

      <asp:Label id="Label2"
           runat="server"/>

   </form>

</body>

</html>

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>

<head>

   <script runat="server">

      Sub Button_Click (sender As Object, e As EventArgs)

         ' Display the selected items.
         Label1.Text = "You selected:"

         Dim i As Integer

         For i=0 To Select1.Items.Count - 1
  
            If Select1.Items(i).Selected Then
               Label1.Text &= "<br> &nbsp;&nbsp; -" & Select1.Items(i).Text
            End If      

         Next i

      End Sub

      Sub Server_Change(sender As Object, e As EventArgs)

         ' The ServerChange event is commonly used for data validation.
         ' This method will display a warning if the "All" option is 
         ' selected in combination with another item in the list.
         Dim Count As Integer = 0
         Dim i As Integer

         ' Determine the number of selected items in the list.
         For i=0 To Select1.Items.Count - 1
  
            If Select1.Items(i).Selected Then

               Count = Count + 1      

            End If

         Next i

         ' Display an error message if more than one item is selected with
         ' the "All" item selected.
         If ((Count > 1) And (Select1.Items(0).Selected)) Then
     
            Label2.Text = "Hey! You can't select 'All' with another selection!!"
         
         Else
         
            Label2.Text = ""
         
         End If

      End Sub

      Sub Page_Load(sender As Object, e As EventArgs)
      
         ' Create an EventHandler delegate for the method you want to 
         ' handle the event, and then add it to the list of methods
         ' called when the event is raised.
         AddHandler Select1.ServerChange, AddressOf Server_Change
         AddHandler Button1.ServerClick, AddressOf Button_Click
         
      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect ServerChange Example </h3>

      Select items from the list: <br><br>

      <select id="Select1" 
              Multiple="True"
              runat="server">

         <option value="All"> All </option>
         <option value="1" Selected="True"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <br><br>

      <button id="Button1"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

      <br>

      <asp:Label id="Label2"
           runat="server"/>

   </form>

</body>

</html>


[C#] 

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

<html>

<head>

   <script runat="server">

      void Button_Click (Object sender, EventArgs e)
      {

         // Display the selected items.
         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; -" + Select1.Items[i].Text;      

         }

      }

      void Server_Change(Object sender, EventArgs e)
      {

         // The ServerChange event is commonly used for data validation.
         // This method will display a warning if the "All" option is  
         // selected in combination with another item in the list.
         int Count = 0;

         // Determine the number of selected items in the list.
         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
  
            if (Select1.Items[i].Selected)
               Count++;      

         }

         // Display an error message if more than one item is selected with
         // the "All" item selected.
         if ((Count > 1) && (Select1.Items[0].Selected))
         {
            Label2.Text = "Hey! You can't select 'All' with another selection!!";
         }
         else
         {
            Label2.Text = "";
         }

      }

      void Page_Load(Object sender, EventArgs e)
      {
      
         // Create an EventHandler delegate for the method you want to
         // handle the event, and then add it to the list of methods
         // called when the event is raised.
         Select1.ServerChange += new System.EventHandler(this.Server_Change);
         Button1.ServerClick += new System.EventHandler(this.Button_Click);
         
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Server Change Example </h3>

      Select items from the list: <br><br>

      <select id="Select1" 
              Multiple="True"
              runat="server">

         <option value="All"> All </option>
         <option value="1" Selected="True"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <br><br>

      <button id="Button1"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

      <br>

      <asp:Label id="Label2"
           runat="server"/>

   </form>

</body>

</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlSelect Class | HtmlSelect Members | System.Web.UI.HtmlControls Namespace | OnServerChange | System.EventHandler

Show: