如何从 ASP.Net 中的用户控件调用客户端脚本?

How to call client script from User Control in ASP.Net?

我已经阅读了很多关于这个问题的参考资料,但没有得到结果。所以,我想让我试着在这里解释一下我的科学。

这是我的 3 脚本,在 view.ascx 控制下。我想在 .cs 文件函数和点击事件中调用它。

view.ascx

<script type="text/javascript">
function startLoading() {
    alert("load start");
    var el = jQuery(".scp-entry-header");
    App.blockUI(el);
}


function stopLoading() {
    alert("load end");
    var el = jQuery(".scp-entry-header");
    App.unblockUI(el);
}

function getCalendarData() {
    alert("calendarcalled");
}
</script>

view.ascx.cs

//ViewData button click
protected void btnGetData_Click(object sender, EventArgs e)
{
    //How to call startLoading function here?
    process from database;
    //How to call stopLoading function here?
}

//simple function call
protected void showCalendar()
{
    //How to call getCalendarData function here?
}

我已经阅读了有关 RegisterScript 的内容,但没有得到结果。我不明白在哪里 RegisterScript 以及如何在函数中调用它。

任何人都可以详细建议我如何在这里调用 javascript 函数吗?

请记住,您不能在 C# 代码中间调用 JavaScript 函数并继续执行服务器端代码。

.cs 文件上对 JavaScript 函数的所有调用只是在服务器端代码执行后执行的 scheduling/marking。它们不会与服务器代码一起执行。

换句话说,您必须使用 UpdatePannels 和异步代码来实现它才能工作,或者更好的是,在 JavaScript.[=11= 中将其实现为异步调用]

客户端的代码应该在客户端上。尽量不要混用。

试试这个

 protected void btnGetData_Click(object sender, EventArgs e)
 {
        //How to call startLoading function here?
        process from database;
        //How to call stopLoading function here?
       Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "startLoading", true);
 }  

 ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "<script>alert('Hello.')</script>", false);