2011-02-09 160 views
2

我有一个在页面加载事件上动态创建的ASP表。在这种情况下,我使用标题填充ASP表格,然后使用指向.ashx页面的ASP .NET Hyperlink控件来为客户端下载文件。来自ASP .NET的Javascript函数调用超链接NavigateURL属性

对于特定的文件(图像文件),我想启动一个javascript函数来打开一个显示该文件的新窗口。我有所有的代码来做到这一点,但我无法让我的Javascript函数在超链接NavigateURL属性中工作。我对JavaScript很陌生,所以我不确定我错过了什么。我可以做我想做的事吗?我可以不使用桌子控制吗?

ASP代码背后

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim iWineID As Integer 

    If Not Integer.TryParse(Request.Params("WineID"), iWineID) Then Throw New InvalidOperationException("Invalid request") 
    Me.lblWineName.Text = Utils.GetWineName(iWineID) 
    Dim dtDocs As New dsDocs.docsDataTable 

    Using taDocs As New dsDocsTableAdapters.docsTableAdapter 
     dtDocs = taDocs.GetDataByProdIDOrWineID((Utils.GetProducerIDByWineID(iWineID)), True, iWineID) 
    End Using 

    If dtDocs.Rows.Count = 0 Then 
     Me.lblDocsFound.Text = "No documents available for this wine." 
    Else 
     Me.NumberDocs(dtDocs) 

     For Each drDoc As dsDocs.docsRow In dtDocs 
      Dim myRow As New TableRow 
      Dim myTitleCell As New TableCell 
      Dim myDLCell As New TableCell 
      Dim myHL As New HyperLink 
      Select Case drDoc.doc_type_id 
       'window.open('preview.aspx?WineID=' + nWineID', 'height=' + nWindowHeight + ',width=' + nWindowWidth + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=' + bScrollbars + ',resizable=' + bScrollbars + ',titlebar=no'); 
       Case Constants.DocType.BottleShot, Constants.DocType.Label, Constants.DocType.Logo 
        myHL.NavigateUrl = "javascript:OpenPrev('" & drDoc.doc_id & "');return false;" '"javascript:window.open('~/Home/docpreview.aspx?DocID=" & drDoc.doc_id '& "','_blank', 'height=600, width=600,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,titlebar=no'" 
        '"~/Home/docpreview.aspx?DocID=" & drDoc.doc_id 
        myHL.Text = "View" 
       Case Else 
        myHL.NavigateUrl = "~/Home/docs.ashx?DocID=" & drDoc.doc_id 
        myHL.Text = "Download" 
      End Select 

      myTitleCell.Text = StrConv(drDoc.doc_type_name, VbStrConv.ProperCase) 
      myDLCell.Controls.Add(myHL) 
      myRow.Cells.Add(myTitleCell) 
      myRow.Cells.Add(myDLCell) 
      Me.tableDocs.Rows.Add(myRow) 
     Next 
    End If 
End Sub 

的Javascript

function OpenPrev(DocID){ 

var objWin 
var myURL 

alert("GO!"); 
myURL='~/Home/docpreview.aspx?DocID=' + DocID; 
objWin=window.open(myURL, 'Doc View', 'width=600,height=600,resizable=no,scrollbars=yes,toolbar=no'); 
} 
+1

没有挖得太深,只是一个快速件事:JavaScript不理解的概念` 〜“ - 这是一个.NET概念,仅在服务器代码中提供。 – 2011-02-09 14:35:47

回答

0

@JoeEnos - 这绝对是它的一部分。尝试使用诸如“http://www.google.com”之类的内容作为myURL并参阅。怎么了。

并查看HTML页面的来源并查看生成的超链接的样子。 href属性可能看起来不对。

+0

我通过尝试另一个网址得到了这个工作。我再次尝试了,它工作。我在工作时使用了Firefox,然后我使用了Chrome浏览器,并且它没有任何问题 - 但是没有使用Firefox的骰子。任何想法为什么这可能是? – user576838 2011-02-11 04:35:15

2

您需要先解析网址。

试着改变你设置NavigateURL喜欢的任何地方:

myHL.NavigateUrl = "~/Home/docs.ashx?DocID=" & drDoc.doc_id 

要:

myHL.NavigateUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath + "Home/docs.ashx?DocID=" & drDoc.doc_id