This is a migrated thread and some comments may be shown as answers.
Call java script function from code behind on Toolbar button click event
1 Answer 81 Views
This is a migrated thread and some comments may be shown as answers.
Tracy
Top achievements
Rank 1
Tracy asked on 08 Jan 2011, 06:27 AM
Hi,

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)
    
scriptManager.RegisterPostBackControl(Button1)
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.

Dim

 

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

 

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.

ASPX PAGE
<%@ 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) {
           alert(strMsg);
       }
         
</script>
  
    <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" >
            <ContentTemplate>
                <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"   >
                    <Items>
                        <telerik:RadToolBarButton runat="server" CommandName="tbtExportPDF"          ImageUrl="~/App_Themes/Images/Applications/Export PDF 16.png"    Text="Export To PDF"   />
                    </Items>
                </telerik:RadToolBar>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:panel>
</asp:Panel>
</asp:Content>

CODE BEHIND
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"
                ExportPDF()
                Exit Select
        End Select
  
    End Sub
    Protected Sub ExportPDF()
  
        Dim strBuilder As New StringBuilder()
        strBuilder.Append("<script laungauge='javascript'>")
        strBuilder.Append("ShowMessage('Hi')")
        strBuilder.Append("</script>")
        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
        ExportPDF()
    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)
        scriptManager.RegisterPostBackControl(Button1)
        scriptManager.RegisterPostBackControl(rtb)
  
    End Sub
End Class

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar Terziev
Telerik team
answered on 12 Jan 2011, 05:22 PM
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.
Asked by
Tracy
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Share this question
or