来自页面* .aspx.cs调用javascript函数

时间:2012-02-24 15:45:39

标签: c# javascript asp.net button

我想从我的代码隐藏中调用javascript函数。 在我的按钮单击事件处理程序中,我有:

protected void load_data_Click(object sender, EventArgs e)
{
    if (dt.Rows.Count == 1)
        {
            BindDl();                       
        }
        else
        {
            //if dt.rows.count! = 1 I want to call a JavaScript function where be one alert! how to do?
        }
}

2 个答案:

答案 0 :(得分:2)

This page will be helpful for you

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();
String csName = "MyAlertFunction";

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csName))
{
  String jsFunction = "yourFunctionHere()";
  cs.RegisterStartupScript(cstype, csName, jsFunction, true);
}

答案 1 :(得分:1)

用户脚本管理器

    ScriptManager.RegisterStartupScript(this, typeof(string), "SHOW_ALERT",  "alert('')", true);

代替警报你可以把你的javascript代码,下一个参数true自动放入脚本标签,所以你不必写它们。