I have a visual web part. In it i use a hyperlink's click event to call an Application page's WebMethod using Ajax.

public static void EditDocMethod()

In this static method i call an non-static method by creating an object of the application page as:

MyAplicationpPage pageObject = new MyAplicationpPage();
        pageObject.myHyperLink.ID = "DocEditLink";
        pageObject.myHyperLink.Visible = true;
        pageObject.myHyperLink.Text = " ";

In this web method i locate the SPFile which i need to download. The problem is the downloading part.

I have a HyperLink in this Application page which i am trying to use for downloading. The approach was to simulate a click event so that the doc will be downloaded.

Tryed below code with no luck.

myHyperLink.NavigateUrl = string.Concat(web.Url, @"/", DocUrl);
myHyperLink.Attributes.Add("onclick", "return DispEx(this,event,'TRUE','FALSE','FALSE', 'SharePoint.OpenDocuments.3','1', 'SharePoint.OpenDocuments', '','','','1','0','0','0x7fffffffffffffff')");

string injectScript = @"<script type='text/javascript'>
document.getElementById('" + myHyperLink.ClientID + @"').click();

ScriptManager.RegisterStartupScript(this, this.GetType(), "injectScript", injectScript, false);

The RegisterStartupScript does not work. Any ideas or better ways of doing this?

2 Answers 2


If I understood what you are doing correctly, I don't think that setup will work. The application page isn't actually being loaded to the browser and thus, the link and javascript will not be available to the user.

Ideally you should move the web method to a proper web service structure instead of having it hang off of the application page. If you can't do that, try changing the web method to return the path to the document and have the web part's code make the file open call.

  • Thanks for the reply. As you said I returned the document path to the calling page and opened it from there using a simple window.open() :)
    – Shaamil
    Jan 16, 2015 at 5:09

Try creating a function and call that the "ScriptManager.RegisterStartupScript". For example:

string script = @"<script language='Javascript'>function callAlert(){alert('...');}callAlert();</script>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.