简体   繁体   中英

ASP.NET AJAX: Creating a postback

I would like to cause a post back that occurs only once by inserting some AJAX into the page after a specific event occurs.

Currently I have:

string script = "<script language='Javascript'>" +
                                    "__doPostBack('GetSpreadsheet', '');" +
                                "</script>";

Page.ClientScript.RegisterStartupScript(this.GetType(), "DownloadExcel", script);

However, after I do this, every post back has an event target of GetSpreadsheet instead of just the first one. What am I doing wrong?

Try using ScriptManager.RegisterStartupScript Method (Control, Type, String, String, Boolean) .

In the remarks, you will find this text which explains what you are after.

Startup script blocks that are registered by using this method are sent to the page only when the control that is registering the block is inside an UpdatePanel control that is being updated.

Try checking the IsStartUpScriptRegistered() method before registering your script.

if (!(Page.ClientScript.IsStartupScriptRegistered("DownloadExcel")) )
     Page.ClientScript.RegisterStartupScript(this.GetType(), "DownloadExcel", script);

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