简体   繁体   中英

Script inside update panel is not working in asp.net

I am using some scripts in my .aspx pages. It was working before I put update panel in these pages. I need both things, update panel and scripts. How to enable these scripts inside the update panel in asp.net? I am using c# and asp.net 4.5 framework. The scripts that I used in the pages are:

    1.

Response.Write("alert('Successfully Updated')");

2.

string url = "./Quick.aspx";
   string cmd = "window.open('" + url + "', '_blank', 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=no,titlebar=no' );";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
   }

These codes are used in the .aspx.cs pages.

Any idea about this issue to solve?

Thanks....

// Is a simply way.

//Common.Response.Write("<Script>alert('Successfully Updated')</Script>");

    string url = "Quick.aspx";
    string cmd = "alert('Successfully Updated');window.open('" + url + "', '_blank', 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=no,titlebar=no' );";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);

将Javascript放在UpdatePanel外部,它将起作用...

In order to fix the first script (1), you cannot use Response.Write as this code could be executed in an async postback, and this is what you have experienced. The solution is to use ScriptManager.RegisterStartupScript as follows:

ScriptManager.RegisterStartupScript(
                        this,
                        this.GetType(),
                        "SuccessAlert",
                        "alert('Successfully Updated');",
                        true);

You can find some more information about the RegisterStartupScript here: https://msdn.microsoft.com/en-us/library/bb359558(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM