Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q2 2011 released 07/12/2011
select

ToolBar / Client-side Events

Configuration
Logged events:
Event log:

  • RadToolBar provides a number of client-side events that would allow you to accomplish even the most complicated tasks.

    You can attach to the RadTollBar's events by using either the server-side properties or the client-side API.

    1. Attaching event handlers via a server-side property
      Each client-side event has a corresponding property, whose name is formed like OnClient[EventName].
      You can set the property to the name of the function to be called when the event occurs.

              <script type="text/javascript">
                  function onButtonClicked(sender, args)
                  {
      	            alert(String.format("Button with text: '{0}' clicked", args.get_item().get_text()));
                  }
              </script>
      
              <telerik:RadToolBar ID="RadToolBar1" runat="server"
                  OnClientButtonClicked="onButtonClicked">
              </telerik:RadToolBar>
      		
    2. Using the client-side API to attach event handlers
      Using the client-side API of RadToolBar allows you to attach multiple event handlers to one event using the standard MS AJAX conventions.
      <script type="text/javascript">
      	function onClickedHandler1()
      	{
      		alert("First handler called");
      	}
      	function onClickedHandler2()
      	{
      		alert("Second handler called");
      	}
      
      	function pageLoad()
      	{
      		var toolBar = $find("<%=RadToolBar1.ClientID%>");
      		
      		toolBar.add_buttonClicked(onClickedHandler1);
      		toolBar.add_buttonClicked(onClickedHandler2);
      	}
      </script>
      
      Another advantage of the client-side API is the possiblity of dynamically detaching certain handlers. You can use the remove_buttonClicked method of the toolbar object.

      <script type="text/javascript">
      	$find(<%=RadToolBar1.ClientID%>).remove_buttonClicked(onClickedHandler1);
      </script>
      
    3. Cancelling an event
      The events, whose names end in ing, can be cancelled. You can cancel the event by using the set_cancel method of the event arguments passed to the handler.
      <script type="text/javascript">
      	function onClientButtonClicking(sender, eventArgs)
      	{
      	    eventArgs.set_cancel(true);
      	}
      </script> 

    There are several important points about button checking:

    • The CheckedStateChanging and CheckedStateChanged events are fired when a button changes its checked state to On or Off
    • There can be only one button (in a group), whose checked state is On. When a button checked state is set to On, the CheckedStateChanging and CheckedStateChanged events (for the button), whose checked state changes to Off, are also fired
    • Clicking on a checked button does not change its checked state to Off by default. Use the AllowSelfUnCheck property to turn this behavior on

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.Web.Examples.ToolBar.ClientSide.ClientEvents.DefaultCS" %>

    <%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
    <%@ Register TagPrefix="qsf" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <qsf:HeadTag runat="server" ID="Headtag1" />
    </head>
    <body class="BODY">
        <form runat="server" id="mainForm" method="post">
            <telerik:RadScriptManager ID="ScriptManager" runat="server" />
            <qsf:Header runat="server" ID="Header1" NavigationLanguage="C#" />

            <script type="text/javascript">
            //<![CDATA[

                function onClientToolBarLoad(sender, args)
                {
                     logEvent("OnClientLoad: " + sender.get_id());
                }

                function clientButtonClicking(sender, args)
                {
                     logEvent("OnClientButtonClicking: " + args.get_item().get_text());
                }

                function clientButtonClicked(sender, args)
                {
                     logEvent("OnClientButtonClicked: " + args.get_item().get_text());
                }

                function clientDropDownOpening(sender, args)
                {
                     logEvent("OnClientDropDownOpening: " + args.get_item().get_text());
                }

                function clientDropDownOpened(sender, args)
                {
                     logEvent("OnClientDropDownOpened: " + args.get_item().get_text());
                }

                function clientDropDownClosing(sender, args)
                {
                     logEvent("OnClientDropDownClosing: " + args.get_item().get_text());
                }

                function clientDropDownClosed(sender, args)
                {
                     logEvent("OnClientDropDownClosed: " + args.get_item().get_text());
                }

                function clientContextMenu(sender, args)
                {
                     logEvent("OnClientContextMenu: " + args.get_item().get_text());
                }

                function clientMouseOver(sender, args)
                {
                     logEvent("OnClientMouseOver: " + args.get_item().get_text());
                }

                function clientMouseOut(sender, args)
                {
                     logEvent("OnClientMouseOut: " + args.get_item().get_text());
                }
                
                function clientCheckedStateChanging(sender, args)
                {
                    logEvent("OnClientCheckedStateChanging: " + args.get_item().get_text());
                }

                function clientCheckedStateChanged(sender, args)
                {
                    logEvent("OnClientCheckedStateChanged: " + args.get_item().get_text());
                }

            //]]>
            </script>

            <div style="overflow: hidden">
         <qsf:ConfiguratorPanel runat="server" ID="ConfigurationPanel1" Title="Configuration" Orientation="Vertical" Expanded="true">
                 Logged events:<br />
                 <asp:CheckBoxList ID="cblLoggedEvents" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cblLoggedEvents_SelectedIndexChanged">
                     <asp:ListItem Text="OnClientLoad" Selected="true" />
                     <asp:ListItem Text="OnClientButtonClicking" Selected="true" />
                     <asp:ListItem Text="OnClientButtonClicked" Selected="true" />
                     <asp:ListItem Text="OnClientDropDownOpening" Selected="true" />
                     <asp:ListItem Text="OnClientDropDownOpened" Selected="true" />
                     <asp:ListItem Text="OnClientDropDownClosing" Selected="true" />
                     <asp:ListItem Text="OnClientDropDownClosed" Selected="true" />
                     <asp:ListItem Text="OnClientContextMenu" Selected="true" />
                     <asp:ListItem Text="OnClientMouseOver" Selected="true" />
                     <asp:ListItem Text="OnClientMouseOut" Selected="true" />
                     <asp:ListItem Text="OnClientCheckedStateChanging" Selected="true" />
                     <asp:ListItem Text="OnClientCheckedStateChanged" Selected="true" />
                 </asp:CheckBoxList>
             </qsf:ConfiguratorPanel>
                
             <telerik:RadToolBar ID="RadToolBar1" runat="server"
             EnableRoundedCorners="true" EnableShadows="true"
                 OnClientLoad="onClientToolBarLoad"
                 OnClientButtonClicking="clientButtonClicking"
                 OnClientButtonClicked="clientButtonClicked"
                 OnClientDropDownOpening="clientDropDownOpening"
                 OnClientDropDownOpened="clientDropDownOpened"
                 OnClientDropDownClosing="clientDropDownClosing"
                 OnClientDropDownClosed="clientDropDownClosed"
                 OnClientContextMenu="clientContextMenu"
                 OnClientMouseOver="clientMouseOver"
                 OnClientMouseOut="clientMouseOut"
                 OnClientCheckedStateChanging="clientCheckedStateChanging"
                 OnClientCheckedStateChanged="clientCheckedStateChanged"
                 >
                 <Items>
                     <telerik:RadToolBarButton Text="Save" />
                     <telerik:RadToolBarButton Text="Load" />
                     <telerik:RadToolBarDropDown Text="Format">
                         <Buttons>
                             <telerik:RadToolBarButton Text="Bold" CheckOnClick="true" Group="FormatBold" Checked="true" />
                             <telerik:RadToolBarButton Text="Italic" CheckOnClick="true" Group="FormatItalic" />
                             <telerik:RadToolBarButton Text="Underline" CheckOnClick="true" Group="FormatUnderline" />
                         </Buttons>
                     </telerik:RadToolBarDropDown>
                     <telerik:RadToolBarSplitButton Text="Apply Color (Red)" EnableDefaultButton="false">
                         <Buttons>
                             <telerik:RadToolBarButton Text="Red" CheckOnClick="true" Group="ApplyColor" Checked="true" />
                             <telerik:RadToolBarButton Text="Blue" CheckOnClick="true" Group="ApplyColor" />
                             <telerik:RadToolBarButton Text="Yellow" CheckOnClick="true" Group="ApplyColor" />
                         </Buttons>
                     </telerik:RadToolBarSplitButton>
                 </Items>
             </telerik:RadToolBar>
            </div>
            
            <qsf:EventLogConsole runat="server" ID="EventLogConsole1" Height="250px" />
            
            <qsf:Footer runat="server" ID="Footer1" />
        </form>
    </body>
    </html>

Get more than expected!

 
 

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2024 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451