In my ASP.NET Web Forms I would like to get the DataNavigateUrlFields from the HyperLinkField control.
In order to get the control I use the following code in the RowDataBound event:
If e.Row.Cells(n).Controls(0).GetType().ToString() = "System.Web.UI.WebControls.HyperLink" Then
Dim linkDownload As HyperLink = DirectCast(e.Row.Cells(n).Controls(0), HyperLink)
The problem is that I get an HyperLink already "filled" so the
DataNavigateURLFormatString (myPage.aspx?downloadId = {0}) and
DataNavigateUrlFields (downloadGuid) are already combined in the
HyperLink.NavigateUrl property.
Is there any way to get the DataNavigateUrlFields property? In case there is not, is there any function to extract the
downloadGuid value from HyperLink.NavigateUrl (in short treating the
NavigateUrl as a QueryString)?
hi, to get query string value you can refer below code or you can alos do string manipulation
If e.Row.RowType = DataControlRowType.DataRow Then
Dim linkDownload As HyperLink = DirectCast(e.Row.Cells(6).Controls(0), HyperLink)
Dim baseUri As New Uri(Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd("/") + "/")
Dim uri As New Uri(baseUri, linkDownload.NavigateUrl)
Response.Write(uri.Query)
Response.Write("</br>")
Response.Write(uri.Query.Split("=")(1)) ' to get actual value
End If
Note : cell index may vary based on your grid Or simply use below string split code
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim linkDownload As HyperLink = DirectCast(e.Row.Cells(6).Controls(0), HyperLink)
'using split
Response.Write(linkDownload.NavigateUrl.Split("?")(1))
End If
End Sub
You will find the Cell the HyperLink is in is actually a DataControlFieldCell. Cast to that, and you can get the ContainingField. That will be the HyperLinkField. Cast to that, and you can get to all its properties.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Thanks both! I am now using the split method which is fast but it looks "dirty" in this case. Moreover I am always eager to deepen my knowledge with GridViews
I then tried the code below, it does not give errors at compile time (also intellisense works) but at runtime launches an exception
System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.HyperLink' to type 'System.Web.UI.WebControls.DataControlFieldCell'.
Dim dataControlDownload1 As DataControlFieldCell = DirectCast(e.Row.Cells(n).Controls(0), DataControlFieldCell)
Dim downloadLink As HyperLinkField = DirectCast(dataControlDownload1.ContainingField, HyperLinkField)
Dim param = downloadLink.DataNavigateUrlFields(0)
Dim dataControlDownload1 As DataControlFieldCell = DirectCast(e.Row.Cells(n).Controls(0), DataControlFieldCell)
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
TRINAKRIAE
Member
211 Points
147 Posts
How to programmatically get the DataNavigateUrlFields property from a HyperLinkField control
Mar 01, 2012 12:21 PM|LINK
In my ASP.NET Web Forms I would like to get the DataNavigateUrlFields from the HyperLinkField control.
In order to get the control I use the following code in the RowDataBound event:
If e.Row.Cells(n).Controls(0).GetType().ToString() = "System.Web.UI.WebControls.HyperLink" Then Dim linkDownload As HyperLink = DirectCast(e.Row.Cells(n).Controls(0), HyperLink)The problem is that I get an HyperLink already "filled" so the DataNavigateURLFormatString (myPage.aspx?downloadId = {0}) and DataNavigateUrlFields (downloadGuid) are already combined in the HyperLink.NavigateUrl property.
Is there any way to get the DataNavigateUrlFields property? In case there is not, is there any function to extract the downloadGuid value from HyperLink.NavigateUrl (in short treating the NavigateUrl as a QueryString)?
karthicks
All-Star
31376 Points
5421 Posts
Re: How to programmatically get the DataNavigateUrlFields property from a HyperLinkField control
Mar 01, 2012 12:57 PM|LINK
hi, to get query string value you can refer below code or you can alos do string manipulation
If e.Row.RowType = DataControlRowType.DataRow Then Dim linkDownload As HyperLink = DirectCast(e.Row.Cells(6).Controls(0), HyperLink) Dim baseUri As New Uri(Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd("/") + "/") Dim uri As New Uri(baseUri, linkDownload.NavigateUrl) Response.Write(uri.Query) Response.Write("</br>") Response.Write(uri.Query.Split("=")(1)) ' to get actual value End IfNote : cell index may vary based on your grid Or simply use below string split code Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim linkDownload As HyperLink = DirectCast(e.Row.Cells(6).Controls(0), HyperLink) 'using split Response.Write(linkDownload.NavigateUrl.Split("?")(1)) End If End SubKarthick S
superguppie
All-Star
48225 Points
8679 Posts
Re: How to programmatically get the DataNavigateUrlFields property from a HyperLinkField control
Mar 01, 2012 02:23 PM|LINK
You will find the Cell the HyperLink is in is actually a DataControlFieldCell. Cast to that, and you can get the ContainingField. That will be the HyperLinkField. Cast to that, and you can get to all its properties.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
TRINAKRIAE
Member
211 Points
147 Posts
Re: How to programmatically get the DataNavigateUrlFields property from a HyperLinkField control
Mar 01, 2012 02:49 PM|LINK
Thanks both! I am now using the split method which is fast but it looks "dirty" in this case. Moreover I am always eager to deepen my knowledge with GridViews
I then tried the code below, it does not give errors at compile time (also intellisense works) but at runtime launches an exception System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.HyperLink' to type 'System.Web.UI.WebControls.DataControlFieldCell'.
Am I doing something wrong?
superguppie
All-Star
48225 Points
8679 Posts
Re: How to programmatically get the DataNavigateUrlFields property from a HyperLinkField control
Mar 01, 2012 03:09 PM|LINK
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.