大家在開發中經常會遇到這樣的情況:點擊一個按鈕執行一個服務端操作,如:添加數據,添加成功後需要彈出一個提示框來提醒客戶。代碼如下:
private void Button2_Click(object sender, System.EventArgs e)
{
String scriptString = "<script language=javascript>";
scriptString += "alert('數據寫入成功!')";
scriptString += "</script>";
if(!this.IsStartupScriptRegistered("Startup"))
this.RegisterStartupScript("Startup", scriptString);
}
再進一步,可以實現提示後跳轉到其它頁面
private void Button2_Click(object sender, System.EventArgs e)
{
String scriptString = "<script language=javascript>";
scriptString += "alert('數據寫入成功!');";
scriptString += "window.location='test.aspx';";
scriptString += "</script>";
if(!this.IsStartupScriptRegistered("Startup"))
this.RegisterStartupScript("Startup", scriptString);
}
如果把這個通用函數放到一個公共類中時要注意要把當前的Page作為參數傳遞過去才行
function static void showmessage(System.Web.UI.Page mypage,string Info)
{
String scriptString = "<script language=javascript>";
scriptString += "alert('“+Info+“')";
scriptString += "</script>";
if(!mypage.IsStartupScriptRegistered("Startup"))
mypage.RegisterStartupScript("Startup", scriptString);
}