Вопрос

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);
Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top