Call java script function from code behind on Toolbar button click event

2 posts, 1 answers
  1. Tracy
    Tracy avatar
    123 posts
    Member since:
    Nov 2010

    Posted 08 Jan 2011 Link to this post


    I have a page with a Rad Toolbar on it.  This page is also using a master page.  I am trying to call a java script from code behind on the Toolbar OnButtonClick event.  The toolbar is inside a UpdatePanel.

    When I click the toolbar button the javascript isn't called. 

    To test my page, I added a regular asp:button to the page and put it inside the update panel and called the function, it didn't work. 
    I did some research and found that I needed to trigger a postback on the button, so added the following code to the Load event and then when I clicked the asp button the javascript was executed.




    Private Sub WebForm4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim scriptManager As ScriptManager = scriptManager.GetCurrent(Me.Page)
    End Sub


    Thinking that I should do the same for the toolbar button, I then added the following to the Load event.  But, the javascript still did not run.



    rtb As RadToolBarButton = DirectCast(rtbPDFInvoices.FindButtonByCommandName("tbtExportPDF"), RadToolBarButton)


    I read on the forums about the client events being unattached after ajax update, but I don't think this is my problem.  I am using the OnButtonClick not the OnButtonClientClick.

    Below is a simplified version of what I am trying to do.  In the real world I need to call the java script from code behind because it only gets called if certain conditions exist.

    Thank you for your help.

    <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/IPSMaster.Master" CodeBehind="WebForm4.aspx.vb" Inherits="IPS_Gateway.WebForm4" %>
    <%@ Register assembly="Telerik.Web.UI"      namespace="Telerik.Web.UI"      tagprefix="telerik" %>
    <%@ Register Assembly="AjaxControlToolkit"  namespace="AjaxControlToolkit"  tagprefix="act" %>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphMainContent" runat="server">
      <script type="text/javascript" language="javascript">
          function ShowMessage(strMsg) {
        <asp:Panel ID="pnlPageContent"         runat="server"    cssClass="css_GPC01_Panel_PageContent" >
        <asp:panel ID="pnlPageHeader"                runat="server"     cssClass="css_GPC01_Panel_PageHeading" >    
            <asp:UpdatePanel ID="pnlToolbar"         runat="server" >
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"  />
                    <telerik:RadToolBar ID="rtbPDFInvoices" runat="server"  AutoPostBack="true"          EnableEmbeddedSkins="True" Skin="Black" OnButtonClick="rtbPDFInvoices_ButtonClick"   >
                            <telerik:RadToolBarButton runat="server" CommandName="tbtExportPDF"          ImageUrl="~/App_Themes/Images/Applications/Export PDF 16.png"    Text="Export To PDF"   />

    Imports Telerik.Web.UI
    Partial Public Class WebForm4
        Inherits System.Web.UI.Page
        Protected Sub rtbPDFInvoices_ButtonClick(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarEventArgs) Handles rtbPDFInvoices.ButtonClick
            Dim btn As RadToolBarButton = TryCast(e.Item, RadToolBarButton)
            Select Case btn.CommandName
                Case "tbtExportPDF"
                    Exit Select
            End Select
        End Sub
        Protected Sub ExportPDF()
            Dim strBuilder As New StringBuilder()
            strBuilder.Append("<script laungauge='javascript'>")
            Page.ClientScript.RegisterStartupScript([GetType](), "ToPDF", strBuilder.ToString)
        End Sub
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        End Sub
        Private Sub WebForm4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim scriptManager As ScriptManager = scriptManager.GetCurrent(Me.Page)
            Dim rtb As RadToolBarButton = DirectCast(rtbPDFInvoices.FindButtonByCommandName("tbtExportPDF"), RadToolBarButton)
        End Sub
    End Class

  2. Answer
    Dimitar Terziev
    Dimitar Terziev avatar
    1268 posts

    Posted 12 Jan 2011 Link to this post

    Hi Tracy,

    I've prepared a small example page showing how to call a javascript function from code-behind.
    I hope this would help.

    All the best,
    Dimitar Terziev
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Back to Top