Multiple Control events in ASP.NET using VB.NET

Describes how to handle multiple control action events which raised on the server in ASP.NET.
  • 2237

During the development of web forms, you can use a single method to handle multiple control events. The ASP.NET server control events are raised and handled on the server.

Whenever a web request arrives at the client-side action to the server, a control will raise events on the server as response to the client action. The event is handled by either the page or its child controls, and ASP.NET sends a response back to the client.

Now the sample explores how to handle multiple control action events raised from different button controls in ASP.NET.


Design View

ControlEvt1.gif

Look at the design view above, where we drop two listboxes and four buttons to perform several actions on controls to raise events.


ListBox Tasks

ControlEvt2.gif

You can add, remove or edit the listbox items through ListItem Collection Editor click on Edit Items from ListBox Tasks as shown above.

From the below property window you can change the style formatting of the header of your page to make your application more attractive.
 

ControlEvt3.gif

Code Snippets

<%@ Page Title="Home Page" Language="VB"AutoEventWireup="false"
 CodeFile="Default.aspx.vb" Inherits="_Default" %>
 <html>
 <title>Multiple Control Events</title>
  <head>
   <style type="text/css">
     .style1
     {
      width: 197px;
     }
     .style2
     {
      color: #003399;
     }
  </style>
 </head>
 <script language="VB" runat="server">
  Sub AddBtn_Click(Sender As Object, E As EventArgs)
    If Not (AvailableFonts.SelectedIndex = -1) 
       InstalledFonts.Items.Add(New ListItem(AvailableFonts.SelectedItem.Value))
       AvailableFonts.Items.Remove(AvailableFonts.SelectedItem.Value)
    End If
  End Sub
  Sub AddAllBtn_Click(Sender As Object, E As EventArgs)
    Do While Not (AvailableFonts.Items.Count = 0)
      InstalledFonts.Items.Add(New ListItem(AvailableFonts.Items(0).Value))
      AvailableFonts.Items.Remove(AvailableFonts.Items(0).Value)
    Loop
  End Sub
  Sub RemoveBtn_Click(Sender As Object, E As EventArgs)
    If Not (InstalledFonts.SelectedIndex = -1)
      AvailableFonts.Items.Add(New ListItem(InstalledFonts.SelectedItem.Value))
      InstalledFonts.Items.Remove(InstalledFonts.SelectedItem.Value)
  End Sub
  Sub RemoveAllBtn_Click(Sender As Object, E As EventArgs)
    Do While Not (InstalledFonts.Items.Count = 0)
      AvailableFonts.Items.Add(New ListItem(InstalledFonts.Items(0).Value))
      InstalledFonts.Items.Remove(InstalledFonts.Items(0).Value)
    Loop
  End Sub
 </script>
 <body>
   <h3 class="style2" style="height: 30px; background-color: #E2E2E2">
   <font face="Verdana" 
      style="font-family: Verdana; font-size: small; font-weight: bold;">ASP.NET Multiple Control Action Events</font></h3>
   <hr style="color: #666666">
   <form id="Form1" action="Default.aspx" runat=server>
      <table style="background-color: #E2E2E2">
        <tr>
          <td class="style1" style="font-family: Verdana; font-size: small">
              <b>Available Categories</b>&nbsp;&nbsp; 
          </td>
          <td>
          <!-- Filler -->
          </td>
          <td style="font-family: Verdana; font-size: small">
              <b>Selected Category </b> 
          </td>
          </tr> 
        <tr>
          <td class="style1">
            <asp:listbox id="AvailableFonts" width="187px" runat=server Font-Names="Verdana" 
               Font-Size="Small" Height="115px">
                 <asp:listitem>ADO.NET and Database</asp:listitem>
                 <asp:listitem>ASP.NET and Web</asp:listitem>
                 <asp:listitem>Ajax</asp:listitem>
                 <asp:listitem>VB.NET</asp:listitem>
                 <asp:listitem>GDI+ and Graphics</asp:listitem>
                 <asp:ListItem>Silverlight</asp:ListItem>
                 <asp:ListItem>WPF</asp:ListItem>
                 <asp:ListItem>XAML</asp:ListItem>
             </asp:listbox>
             </td>
             <td>
             <!-- Filler -->
             </td>
             <td>
             <asp:listbox id="InstalledFonts" width="187px" runat=server Font-Names="Verdana" 
                Font-Size="Small" Height="115px">
             </asp:listbox>
               </td>
             </tr> 
             <tr>
               <td class="style1">
              <!-- Filler -->
               </td>
               <td>
                <asp:button ID="Button1" text="<<" OnClick="RemoveAllBtn_Click" runat=server/>
                <asp:button ID="Button2" text="Insert Left" OnClick="RemoveBtn_Click" 
                   runat=server Font-Names="Verdana" Font-Size="Small"/> 
                <asp:button ID="Button3" text="Insert Right" OnClick="AddBtn_Click" runat=server 
                   Font-Names="Verdana" Font-Size="Small"/>
                <asp:button ID="Button4" text=">>" OnClick="AddAllBtn_Click" runat=server/> 
               </td>
               <td>
              <!-- Filler -->
               </td>
            </tr>
         </table>
       </form> 
    </body>
</html>

Output

ControlEvt4.gif
 

We inserted three items from the left listbox to right using insert right button control, Use insert left button to insert it back. The left most button insert all the items from right listbox to left and the right most button insert the left one to right listbox.
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.