想供各頁面引用
結果寫一個簡單的 Javascript Alert() function 就一直死不出來
在這邊記錄一下正確寫法
public void AlertMessage(System.Web.UI.Page page, string ScriptID, string Message)
{
ClientScriptManager cs = page.ClientScript;
if (!cs.IsStartupScriptRegistered(typeof(string), ScriptID))
{
string Script = "<script language=\"javascript\">";
Script += " alert(\"" + Message + "\");";
Script += "</script>"
sc.RegisterStartupScript(typeof(string), ScriptID, Script);
}
}
{
ClientScriptManager cs = page.ClientScript;
if (!cs.IsStartupScriptRegistered(typeof(string), ScriptID))
{
string Script = "<script language=\"javascript\">";
Script += " alert(\"" + Message + "\");";
Script += "</script>"
sc.RegisterStartupScript(typeof(string), ScriptID, Script);
}
}
之前是死在我自行宣告的 page 物件
因為 page 物件是我建構出來的
並不是呼叫頁面的 page
RegisterStartupScript 加入的是我自行宣告的 page 物件中
而非原呼叫頁面
所以原呼叫頁面完全沒有這段 script 內容
原錯誤程式如下:
public void AlertMessage(string ScriptID, string Message)
{
System.Web.UI.Page page = new System.Web.UI.Page();
ClientScriptManager cs = page.ClientScript;
if (!cs.IsStartupScriptRegistered(typeof(string), ScriptID))
{
string Script = "<script language=\"javascript\">";
Script += " alert(\"" + Message + "\");";
Script += "</script>"
sc.RegisterStartupScript(typeof(string), ScriptID, Script);
}
}
{
System.Web.UI.Page page = new System.Web.UI.Page();
ClientScriptManager cs = page.ClientScript;
if (!cs.IsStartupScriptRegistered(typeof(string), ScriptID))
{
string Script = "<script language=\"javascript\">";
Script += " alert(\"" + Message + "\");";
Script += "</script>"
sc.RegisterStartupScript(typeof(string), ScriptID, Script);
}
}
沒有留言:
張貼留言