سؤال

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