C# 使用 ScriptManager.RegisterStartupScript 从后面的代码调用 javascript 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12032466/
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-09 20:08:20  来源:igfitidea点击:

Calling javascript Method from Code behind using ScriptManager.RegisterStartupScript

c#javascriptasp.net

提问by Dharmendra Kumar Singh

I am using Ajax Toolkit on my content page . I have an Javascript Code which i want to call from the code behind using ScriptManager.RegisterStartupScript.

我在我的内容页面上使用 Ajax Toolkit。我有一个 Javascript 代码,我想从使用ScriptManager.RegisterStartupScript.

The javaScript Code is :-

javaScript 代码是:-

<script type="text/javascript">
   function disp_confirm() {
       var r = jConfirm("Your Shift End ! Do you still want to Continue ? ")
       if (r == true) {
           jAlert("You pressed OK!")
       }
       else {
           jAlert("You pressed Cancel!")
       }

How to call this disp_confirm()method of javascript from the code behind using C# .

如何disp_confirm()使用 C# 从后面的代码中调用此javascript 方法。

采纳答案by Rajneesh

Try this

尝试这个

ScriptManager.RegisterStartupScript(Page,GetType(),"disp_confirm","<script>disp_confirm()</script>",false)

回答by user2021837

 System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "Script", "myFun();", true); 

this worked for me

这对我有用

回答by Satinder singh

If one use Asp.net UpdatePanel control from ajax toolkit then we need to use ScriptManager.RegisterStartupScript

如果使用 ajax 工具包中的 Asp.net UpdatePanel 控件,那么我们需要使用 ScriptManager.RegisterStartupScript

Code:

代码:

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:disp_confirm(); ", true);


Detailed Article:

详细文章:

Call Javascript Function from Code Behind in Asp.net C# with/without using Asp.net UpdatePanel control

使用/不使用 Asp.net UpdatePanel 控件从 Asp.net C# 中的代码背后调用 Javascript 函数

回答by Max Alexander Hanna

In short, you cannot fire javascript from codebehind without first having to refresh the page to take into account the javascript code you inject.

简而言之,您不能在不首先刷新页面以考虑您注入的 javascript 代码的情况下从代码隐藏中触发 javascript。

Basically, with RegisterStartupScript, youre telling asp to inject some javascript on page load. but this wont help if you need to dynamically call a javascript function or variable.

基本上,使用RegisterStartupScript,您是在告诉asp 在页面加载时注入一些javascript。但是如果您需要动态调用 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();
            }
        }