page.clientscript.registerstartupscript的代码不起作用

时间:2015-07-08 10:40:19

标签: asp.net

我正在使用Page.ClientScript.RegisterStartupScript();在asp.net C#中点击按钮显示消息。

代码如下所示:

string javaScript1 = "<script language=JavaScript>\n" + "alert('Can't add new entry since course id already exists for the selected year.');\n" + "</script>";
                RegisterStartupScript("image1_ClickScript", javaScript1);

此代码写在按钮单击事件功能中。另一个功能在同一页面上的相同代码工作正常,但它无法正常工作。我已通过调试器检查,没有错误,但警报框不会出现在浏览器上。

任何建议都将在同一方向受到赞赏。

2 个答案:

答案 0 :(得分:0)

在一行中尝试:

Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Can't add new entry since course id already exists for the selected year.');", true);

答案 1 :(得分:-1)

string javaScript1 = "\n" + "alert('Cant add new entry since course id already exists for the selected year.');\n" + ""; RegisterStartupScript("image1_ClickScript", javaScript1);

试试这个。看起来像不能导致问题的单引号。