簡體   English   中英

使用 OnClientClick 打開新頁面 - ASP.Net

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM