简体   繁体   English

使用ScriptManager.RegisterStartupScript从Code后面调用javascript方法

[英]Calling javascript Method from Code behind using ScriptManager.RegisterStartupScript

I am using Ajax Toolkit on my content page . 我在我的内容页面上使用Ajax Toolkit。 I have an Javascript Code which i want to call from the code behind using ScriptManager.RegisterStartupScript . 我有一个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# . 如何使用C#从后面的代码中调用这个javascript的disp_confirm()方法。

尝试这个

ScriptManager.RegisterStartupScript(Page,GetType(),"disp_confirm","<script>disp_confirm()</script>",false)
 System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "Script", "myFun();", true); 

这对我有用

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#中的Code Behind调用Javascript函数

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. 基本上,使用RegisterStartupScript,你告诉asp在页面加载时注入一些javascript。 but this wont help if you need to dynamically call a javascript function or variable. 但是如果您需要动态调用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. 你可以从我的例子中得到什么:我有一个覆盖ASP控件的div,我想要从javascript和codebehind运行。 The div's onClick method AND the calendar's OnSelectionChanged event both get fired this way. div的onClick方法和日历的OnSelectionChanged事件都以这种方式触发。

In this example, i am using an ASP Calendar control, and im controlling it from both javascript and codebehind: 在这个例子中,我使用的是ASP日历控件,并且我可以从javascript和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();
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ScriptManager.RegisterStartupScript调用的客户端方法未触发 - Client method called by ScriptManager.RegisterStartupScript not firing 使用scriptmanager.registerstartupscript显示对话框 - Show dialog using scriptmanager.registerstartupscript ScriptManager.RegisterStartupScript不可用 - ScriptManager.RegisterStartupScript not available ScriptManager.RegisterStartupScript - ScriptManager.RegisterStartupScript 通过ScriptManager.RegisterStartupScript调用时,JavaScript回调函数无法找到控件 - JavaScript callback function unable to find control when calling through ScriptManager.RegisterStartupScript 如何在 ScriptManager.RegisterStartupScript 中包含的 javascript function 中使用 getElementById() 调用 asp 控件 - How to call an asp control using getElementById() in a javascript function that is included in ScriptManager.RegisterStartupScript ScriptManager.RegisterStartupScript()方法不起作用 - ASP.NET,C# - ScriptManager.RegisterStartupScript() method not working - ASP.NET, C# 如何从DLL中使用ScriptManager.RegisterStartupScript - How to use ScriptManager.RegisterStartupScript from within a dll 使用带有js文件的RegisterStartupScript从C#代码后面调用Javascript函数 - Calling Javascript function from C# Code behind using RegisterStartupScript w/ js file ScriptManager.RegisterStartupScript()在IIS上失败,但在Visual Studio中失败 - ScriptManager.RegisterStartupScript() Failling on IIS but not in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM