ARTICLE

How to handle Multiple Control events in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET February 03, 2011
Describes how to handle multiple control action events which raised on the server in ASP.NET.
 
Reader Level:

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

ControlEvt4.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

ControlEvt2.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.
 

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor