ScriptManager.RegisterStartupScript接收两个但执行一个脚本(ScriptManager.RegisterStartupScript receives two but execute one script)

我试图在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);

I´m trying to show two alerts on page_load, but ScriptManager just execute my first one. I realy don´t know what to do, can someone help me?

Here is my test code. It´s located inside the Load method:

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);

最满意答案

正如评论中所提到的,您可能会为两个注册获得相同的密钥。 试试这种方式:

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);

或者将两个脚本合并为一个脚本并只进行一次注册。

As mentioned in the comments, you're probably getting the same key for both registrations. Try this way:

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);

Or merge both scripts into a single one and make just one registration.

ScriptManager.RegisterStartupScript接收两个但执行一个脚本(ScriptManager.RegisterStartupScript receives two but execute one script)

我试图在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);

I´m trying to show two alerts on page_load, but ScriptManager just execute my first one. I realy don´t know what to do, can someone help me?

Here is my test code. It´s located inside the Load method:

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);

最满意答案

正如评论中所提到的,您可能会为两个注册获得相同的密钥。 试试这种方式:

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);

或者将两个脚本合并为一个脚本并只进行一次注册。

As mentioned in the comments, you're probably getting the same key for both registrations. Try this way:

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);

Or merge both scripts into a single one and make just one registration.