[英]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.