How to call javascript function from code-behind

I wrote a javascript with a page.

In Page

     <script type="text/javascript">
      function Myfunction(){
<input type="text" id="MyText" runat="server" /> </BODY>

In Code-behind

 Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  Handles Me.Load
       If Session("My")= "Hi" Then
          I want to call "Myfunction" javascript function
       End If 
 End Sub

How can I do?



This is a way to invoke one or more java script methods from the code behind. By using Script Manager we can call the methods in sequence. Consider the below loc foe example.

ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg", 
   alert('Overrides successfully Updated.');DisableControls();});", true);

In this first method EnableControls() is invoked. Next the alert will be displayed. Next the DisableControls() method will be invoked.


One way of doing it is to use the ClientScriptManager:


There is a very simple way in which you can do this. It involves injecting a javascript code to a label control from code behind. here is sample code:

<head runat="server"> 
    <title>Calling javascript function from code behind example</title> 
        <script type="text/javascript"> 
            function showDialogue() { 
                alert("this dialogue has been invoked through codebehind."); 


lblJavaScript.Text = "<script type='text/javascript'>showDialogue();</script>";

Check out the full code here: (dead)
Link from Internet Archive:


If the order of the execution is not important and you need both some javascript AND some codebehind to be fired on an asp element, heres what you can do.

What you can take away from my example: I have a div covering the ASP control that I want both javascript and codebehind to be ran from. The div's onClick method AND the calendar's OnSelectionChanged event both get fired this way.

In this example, i am using an ASP Calendar control, and im controlling it from both javascript and codebehind:

Front end code:

        <div onclick="showHideModal();">
                OnSelectionChanged="DatepickerDateChange" ID="DatepickerCalendar" runat="server" 
                BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" 
                Font-Size="8pt" ShowGridLines="true" BackColor="#B8C9E1" BorderColor="#003E51" Width="100%"> 
                <OtherMonthDayStyle ForeColor="#6C5D34"> </OtherMonthDayStyle> 
                <DayHeaderStyle  ForeColor="black" BackColor="#D19000"> </DayHeaderStyle>
                <TitleStyle BackColor="#B8C9E1" ForeColor="Black"> </TitleStyle> 
                <DayStyle BackColor="White"> </DayStyle> 
                <SelectedDayStyle BackColor="#003E51" Font-Bold="True"> </SelectedDayStyle> 


        protected void DatepickerDateChange(object sender, EventArgs e)
            if (toFromPicked.Value == "MainContent_fromDate")
                fromDate.Text = DatepickerCalendar.SelectedDate.ToShortDateString();
                toDate.Text = DatepickerCalendar.SelectedDate.ToShortDateString();


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.