Calendar.OnSelectionChanged Metodo

Definizione

Genera l'evento SelectionChanged del controllo Calendar e consente di fornire un gestore personalizzato per l'evento SelectionChanged.

protected:
 virtual void OnSelectionChanged();
protected virtual void OnSelectionChanged ();
abstract member OnSelectionChanged : unit -> unit
override this.OnSelectionChanged : unit -> unit
Protected Overridable Sub OnSelectionChanged ()

Esempio

Nell'esempio di codice seguente viene illustrato come specificare e codificare un gestore per l'evento SelectionChanged per visualizzare il numero di elementi selezionati nel Calendar controllo .

<%@ 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>Calendar SelectionChanged Example</title>
<script runat="server">

      void Selection_Change(Object sender, EventArgs e) 
      {
 
         // Clear the current text.
         Message.Text = "";

         // Iterate through the SelectedDates collection and display the
         // dates selected in the Calendar control.
         foreach(DateTime day in Calendar1.SelectedDates)
         {

            Message.Text += day.Date.ToShortDateString() + "<br />";

         }
         
      }

   </script>

</head>     
<body>

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

      <h3>Calendar SelectionChanged Example</h3>

      Select dates on the Calendar control.<br /><br />

      <asp:Calendar ID="Calendar1" runat="server"  
           SelectionMode="DayWeekMonth" 
           ShowGridLines="True"             
           OnSelectionChanged="Selection_Change">

         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>

      <hr />

      <table border="1">

         <tr style="background-color:silver">

            <th>

               Selected Dates:

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="No dates selected." 
                    runat="server"/>

            </td>

         </tr>

      </table>

   </form>

</body>

</html>
<%@ Page Language="VB" 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>Calendar SelectionChanged Example</title>
<script runat="server">

      Sub Selection_Change(sender As Object, e As EventArgs) 
 
         ' Clear the current text.
         Message.Text = ""

         ' Iterate through the SelectedDates collection and display the
         ' dates selected in the Calendar control.
         Dim day As DateTime

         For Each day In Calendar1.SelectedDates

            Message.Text &= day.Date.ToShortDateString() & "<br />"

         Next
         
      End Sub

   </script>

</head>     
<body>

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

      <h3>Calendar SelectionChanged Example</h3>

      Select dates on the Calendar control.<br /><br />

      <asp:Calendar ID="Calendar1" runat="server"  
           SelectionMode="DayWeekMonth" 
           ShowGridLines="True"             
           OnSelectionChanged="Selection_Change">

         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>

      <hr />

      <table border="1">

         <tr style="background-color:silver">

            <th>

               Selected Dates:

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="No dates selected." 
                    runat="server"/>

            </td>

         </tr>

      </table>

   </form>

</body>

</html>
<%@ 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>Calendar SelectionChanged Example</title>
<script runat="server">

      void Selection_Change(Object sender, EventArgs e) 
      {
 
         // Clear the current text.
         Message.Text = "";

         // Iterate through the SelectedDates collection and display the
         // dates selected in the Calendar control.
         foreach(DateTime day in Calendar1.SelectedDates)
         {

            Message.Text += day.Date.ToShortDateString() + "<br />";

         }
         
      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Manually register the event-handling method for the  
         // SelectionChanged event of the Calendar control.
         Calendar1.SelectionChanged += new EventHandler(this.Selection_Change);

      }


   </script>

</head>     
<body>

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

      <h3>Calendar SelectionChanged Example</h3>

      Select a day, week, or month on the Calendar control.<br /><br />

      <asp:Calendar ID="Calendar1" runat="server"  
           SelectionMode="DayWeekMonth" 
           ShowGridLines="True">

         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>

      <hr />

      <table border="1">

         <tr style="background-color:silver">

            <th>

               Selected dates

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="No dates selected." 
                    runat="server"/>

            </td>

         </tr>

      </table>

   </form>

</body>

</html>
<%@ Page Language="VB" 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>Calendar SelectionChanged Example</title>
<script runat="server">

      Sub Selection_Change(sender As Object, e As EventArgs) 
 
         ' Clear the current text.
         Message.Text = ""

         ' Iterate through the SelectedDates collection and display the
         ' dates selected in the Calendar control.
         Dim day As DateTime

         For Each day In Calendar1.SelectedDates

            Message.Text &= day.Date.ToShortDateString() & "<br />"

         Next
         
      End Sub

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Manually register the event-handling method for the   
         ' SelectionChanged event of the Calendar control.
         AddHandler Calendar1.SelectionChanged, AddressOf Selection_Change

      End Sub

   </script>

</head>     
<body>

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

      <h3>Calendar SelectionChanged Example</h3>

      Select a day, week, or month on the Calendar control.<br /><br />

      <asp:Calendar ID="Calendar1" runat="server"  
           SelectionMode="DayWeekMonth" 
           ShowGridLines="True">

         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>

      <hr />

      <table border="1">

         <tr style="background-color:silver">

            <th>

               Selected Dates:

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="No dates selected." 
                    runat="server"/>

            </td>

         </tr>

      </table>

   </form>

</body>

</html>

Commenti

L'evento SelectionChanged viene generato quando l'utente seleziona un giorno, una settimana o un intero mese facendo clic sui controlli selettore data.

Quando viene generato un evento, il gestore dell'evento viene richiamato tramite un delegato. Per altre informazioni, vedere la gestione e generazione di eventi.

Il metodo OnSelectionChanged consente inoltre alle classi derivate di gestire l'evento senza associare un delegato. È la tecnica consigliata per la gestione dell'evento in una classe derivata.

Note per gli eredi

Quando si esegue l'override di OnSelectionChanged() in una classe derivata, verificare di chiamare il metodo OnSelectionChanged() della classe di base in modo che i delegati registrati ricevano l'evento.

Si applica a

Vedi anche