Javascript function call from ASP .NET Hyperlink NavigateURL property
I have a ASP table that I create dynamically on the page load event. In that event, I populate the ASP table with titles and then a ASP .NET Hyperlink control that points to a .ashx page to serve up a file for the client to download.
For particular files (image files), I'd like to kick off a javascript function to open a new window with that file displayed in it. I have all the code to do this, but I can't get my Javascript function to work in the hyperlink NavigateURL property. I'm very new to Javascript, so I'm not sure what I'm missing. Can I do what I'm trying to? Can I not use the table control?
ASP code behind
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开发者_如何学C 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');
}
You need to first resolve the URL.
Try changing any place where you set the NavigateURL like:
myHL.NavigateUrl = "~/Home/docs.ashx?DocID=" & drDoc.doc_id
To:
myHL.NavigateUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath + "Home/docs.ashx?DocID=" & drDoc.doc_id
@JoeEnos - That's definitely part of it. Try using something like 'http://www.google.com' as myURL
and see. what happens.
And view the source of the HTML page and see what the generated hyperlinks look like. The href
attribute will probably look wrong.
精彩评论