我试图在page_load上显示两个警报,但是ScriptManager只是执行我的第一个警报。 我真的不知道该怎么做,有人可以帮助我吗?
这是我的测试代码。它位于Load方法内:
Page currentPage = (Page)HttpContext.Current.Handler;
ScriptManager.RegisterStartupScript(currentPage, typeof(string), "Script" + DateTime.Now.Millisecond,
"alert('1!');", true);
ScriptManager.RegisterStartupScript(currentPage, typeof(string), "Script" + DateTime.Now.Millisecond,
"alert('2!');", true);
答案 0 :(得分:6)
正如评论中所提到的,您可能会为两个注册获得相同的密钥。试试这个:
Page currentPage = (Page)HttpContext.Current.Handler;
ScriptManager.RegisterStartupScript(currentPage, typeof(string),
"Script" + Guid.NewGuid, "alert('1!');", true);
ScriptManager.RegisterStartupScript(currentPage, typeof(string),
"Script" + Guid.NewGuid, "alert('2!');", true);
或者将两个脚本合并为一个脚本并只进行一次注册。