简体   繁体   中英

Open new page using OnClientClick - ASP.Net

I am working in ASP.Net C#. Trying to invoke Downlaod.aspx page to download a file when a button (added in a gridview) is clicked. Following is code.

<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False"
    OnClientClick='<%# String.Format("window.open(""../../Views/Common/Download.aspx?PropertyDocumentID={0}""); return false;", Eval("DocumentId").ToString())%>' />

When i click it, i can see following error in browser console.

Uncaught SyntaxError: Unexpected token .

But I am unable to figure out syntax error.

One option would be to make a separate js function and use it like below:

<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick='OpenWindow(<%# Eval("DocumentId").ToString() %>)' />

JS function:

function OpenWindow(documentId)
{
    window.open("../../Views/Common/Download.aspx?PropertyDocumentID=" + documentId); 
    return false;
}

Try Below:

Method 1:

Change double quote to single quote

<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick = '<%#String.Format("window.open('../../Views/Common/Download.aspx?PropertyDocumentID={0}'); return false;');",Eval("DocumentId").ToString()) %>' />

Or remove your string.Format and use like this

<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick = '<%#" window.open('../../Views/Common/Download.aspx?PropertyDocumentID=" + Eval("DocumentId").ToString() + "); return false;" %>' />

Method 2:

HTML

<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick='<%# "LocateMyPage(" + Eval("DocumentId").ToString() + ");" %>' />

Javascript

<script type="text/javascript">

 function LocateMyPage(DocID){
     window.open('../../Views/Common/Download.aspx?PropertyDocumentID=' + DocID);
     return false;
 }

</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