[英]Open new page using OnClientClick - ASP.Net
我在 ASP.Net C# 中工作。 当单击按钮(添加到 gridview 中)时,尝试调用 Downlaod.aspx 页面以下载文件。 以下是代码。
<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())%>' />
当我单击它时,我可以在浏览器控制台中看到以下错误。
未捕获的 SyntaxError: Unexpected token
.
但我无法弄清楚语法错误。
一种选择是创建一个单独的 js 函数并使用它,如下所示:
<asp:Button ID="btnViewDocument" runat="server" Text="View" UseSubmitBehavior="False" OnClientClick='OpenWindow(<%# Eval("DocumentId").ToString() %>)' />
JS函数:
function OpenWindow(documentId)
{
window.open("../../Views/Common/Download.aspx?PropertyDocumentID=" + documentId);
return false;
}
试试下面:
方法一:
把双引号改成单引号
<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()) %>' />
或者删除你的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;" %>' />
方法二:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.