从C#中的代码后面运行javascript函数

本文关键字:运行 javascript 函数 代码 | 更新日期: 2023-09-27 18:25:16

在我的代码后面,我有一个搜索函数,我想在它的开头和结尾调用一些javascript。

为了做到这一点,我尝试了几种方法,但没有一种奏效!

以下是我尝试过的:

这个:

this.Page.ClientScript.RegisterStartupScript(this.GetType(), string.Empty, "<script type='text/javascript'>document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block';</script>");

这个:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Wait", "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block';", true);

尽管如此,我还是在.aspx上写了一个函数:

function wait() {
    document.getElementById('light').style.display = document.getElementById('light').style.display == 'block' ? 'none' : 'block';
    document.getElementById('fade').style.display = document.getElementById('fade').style.display == 'block' ? 'none' : 'block';
}

试着这样称呼它:

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Wait", "<script>wait();</script>");

这个:

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Wait", "<script>wait();</script>");

你知道我做错了什么吗?

编辑

我在这里调用函数,if中的一个运行,else中的另一个不

if (someTest())
{
    this.someControl.CssClass = "alert_Class";
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>noCriteriasEnter();</script>");
    this.someControl.Focus();
    return;
}
else
{
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Wait", "<script>wait();</script>");
}

编辑2

最好的是:当我在C#函数(代码后面)的末尾调用函数时,它会运行!!!

从C#中的代码后面运行javascript函数

您的代码中没有任何错误。我以前也这样做过。问题是您是否在客户端代码中使用SCRIPTMANAGER。仅仅在代码后面写scriptmanager是行不通的。

你的HTML中有这一行吗?

<asp:ScriptManager ID="smCharts" runat="server" />

如果没有,请对此进行一些研究,并将其添加到您的标记