How to call javascript function from code

I wrote javascript with an asp.net page.

On the Asp.net page

<HTML> <HEAD> <script type="text/javascript"> function Myfunction(){ document.getElementId('MyText').value="hi"; } </script> </HEAD> <BODY> <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?

+49
javascript
Jan 31 '11 at 7:51
source share
4 answers

This is a way to call one or more java script methods from the code behind. Using the script manager, we can call methods in sequence. Consider the search example below.

 ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg", "$(document).ready(function(){EnableControls(); alert('Overrides successfully Updated.');DisableControls();});", true); 

This first method calls EnableControls (). Then a warning will appear. Then the DisableControls () method is called.

+39
Mar 19 '14 at 9:18
source share

One way to do this is to use the ClientScriptManager :

 Page.ClientScript.RegisterStartupScript( GetType(), "MyKey", "Myfunction();", true); 
+68
Jan 31 '11 at 8:01
source share

There is a very simple way you can do this. This is due to the introduction of javascript code in the label control from the code behind. here is a 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."); } </script> </head> 

..........

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

Check out the full code here: http://softmate-technologies.com/javascript-from-CodeBehind.htm (dead)
Link from the online archive: https://web.archive.org/web/20120608053720/http://softmate-technologies.com/javascript-from-CodeBehind.htm

+13
Jun 02 2018-12-12T00:
source share

If the execution order is not important and you need some kind of javascript AND some code to be run for the asp element, here is what you can do.

What you can take from my example: I have a div spanning an ASP control from which I want to run both javascript and codebehind. OnClick div method And the OnSelectionChanged calendar event both fire in this way.

In this example, I use the ASP Calendar control, and I control it from both javascript and codebehind:

Front End Code:

  <div onclick="showHideModal();"> <asp:Calendar 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> </asp:Calendar> </div> 

Codebehind:

  protected void DatepickerDateChange(object sender, EventArgs e) { if (toFromPicked.Value == "MainContent_fromDate") { fromDate.Text = DatepickerCalendar.SelectedDate.ToShortDateString(); } else { toDate.Text = DatepickerCalendar.SelectedDate.ToShortDateString(); } } 
0
Apr 16 '19 at 15:31
source share



All Articles