2012-02-24 7 views
3

私のコードビハインドからjavascript関数を呼び出したい。from page * .aspx.cs 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

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

ユーザースクリップマネージャー

あなたはいけないので、警告の代わりに使用すると、自動的にスクリプトタグには、次の引数真プットをあなたのjavascriptのコードを置くことができます
ScriptManager.RegisterStartupScript(this, typeof(string), "SHOW_ALERT", "alert('')", true); 

:私の私が持っているボタンのクリックイベントハンドラで それらを書く必要があります。

関連する問題