2012-02-24 72 views
3

我想从我的代码隐藏中调用一个javascript函数。 在我的按钮单击事件处理程序,我有:from page * .aspx.cs call javascript function

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

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

用户纸条经理

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

其中地方警报,你可以把你的JavaScript代码,脚本下一个参数真放自动标记所以你不要必须写他们。