简体   繁体   English

使用 OnClientClick 打开新页面 - ASP.Net

[英]Open new page using OnClientClick - ASP.Net

I am working in ASP.Net C#.我在 ASP.Net C# 中工作。 Trying to invoke Downlaod.aspx page to download a file when a button (added in a gridview) is clicked.当单击按钮(添加到 gridview 中)时,尝试调用 Downlaod.aspx 页面以下载文件。 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 .未捕获的 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:一种选择是创建一个单独的 js 函数并使用它,如下所示:

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

JS function: JS函数:

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或者删除你的string.Format并像这样使用

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

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

Javascript Javascript

<script type="text/javascript">

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

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM