Jeffrey Lott
Jeffrey Lott

Reputation: 7419

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?

Upvotes: 0

Views: 227

Answers (2)

joerage
joerage

Reputation: 4923

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.

Upvotes: 2

Jeff French
Jeff French

Reputation: 1029

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

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

Upvotes: 0

Related Questions