views:

58

answers:

2

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?

A: 

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

if (!(Page.ClientScript.IsStartupScriptRegistered("DownloadExcel")) )
     Page.ClientScript.RegisterStartupScript(this.GetType(), "DownloadExcel", script);
Jeff French
I tried it, but it didn't fix it.
Correl
+2  A: 

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.

joerage
I am using this code in a UserControl. Should I put the user control inside an update panel?
Correl