如何从代码隐藏调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4848678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:24:27  来源:igfitidea点击:

How to call javascript function from code-behind

javascriptasp.net

提问by zanhtet

I wrote a javascript with a asp.net page.

我用asp.net 页面写了一个javascript。

In Asp.net Page

在 Asp.net 页面中

<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?

我能怎么做?

采纳答案by Deepak Kothari

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.

这是一种从后面的代码中调用一个或多个 java 脚本方法的方法。通过使用脚本管理器,我们可以按顺序调用这些方法。考虑下面的 loc foe 示例。

ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg", 
   "$(document).ready(function(){EnableControls();
   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.

在第一个方法中,EnableControls() 被调用。接下来将显示警报。接下来将调用 DisableControls() 方法。

回答by Jacob

One way of doing it is to use the ClientScriptManager:

一种方法是使用ClientScriptManager

Page.ClientScript.RegisterStartupScript(
    GetType(), 
    "MyKey", 
    "Myfunction();", 
    true);

回答by evaaggy

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:

有一种非常简单的方法可以做到这一点。它涉及从后面的代码向标签控件注入 javascript 代码。这是示例代码:

<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 Internet Archive: https://web.archive.org/web/20120608053720/http://softmate-technologies.com/javascript-from-CodeBehind.htm

在此处查看完整代码:http: //softmate-technologies.com/javascript-from-CodeBehind.htm(已失效)
来自 Internet Archive 的链接:https://web.archive.org/web/20120608053720/http: // softmate-technologies.com/javascript-from-CodeBehind.htm

回答by Max Alexander Hanna

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.

如果执行的顺序并不重要,并且您需要在 asp 元素上触发一些 javascript 和一些代码隐藏,那么您可以执行以下操作。

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.

你可以从我的例子中得到什么:我有一个 div 覆盖了我想要运行 javascript 和代码隐藏的 ASP 控件。div 的 onClick 方法和日历的 OnSelectionChanged 事件都以这种方式触发。

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

在这个例子中,我使用了一个 ASP 日历控件,我从 javascript 和代码隐藏中控制它:

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();
            }
        }