C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Code to Provide Hyperlink for the url in a Gridview


Posted Date: 04 Sep 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: RameshMember Level: Silver    
Rating: Points: 10



The following piece of code will display a hyperlink in the gridview's bound column, if it contains a url.


For icnt as integer = 0 to Gridview1.Rows.Count -1

Dim strText As String = Gridview1.Rows(iCnt).Cells(1).Text '("Text containing url to be bound in a gridview")
Dim strOrgText As String = strText

If strText.ToLower.Contains("http:") Or strText.ToLower.Contains("https:") Or strText.ToLower.Contains("www.") Then

strText = strText.Replace(vbCrLf, " ")

Dim strArr() As String = strText.Split(" ")

For iArrCnt As Integer = 0 To strArr.Length - 1

If strArr(iArrCnt).ToLower.Contains("http:") Or strArr(iArrCnt).ToLower.Contains("https:") Then

If strArr(iArrCnt).Contains(".") Then
strOrgText = strOrgText.Replace(strArr(iArrCnt), "<a href='" & strArr(iArrCnt) & "' target='_blank'>" & strArr(iArrCnt) & "</a>")
End If

ElseIf strArr(iArrCnt).ToLower.Contains("www.") And strArr(iArrCnt).Length >= 7 Then

strOrgText = strOrgText.Replace(strArr(iArrCnt), "<a href='http://" & strArr(iArrCnt) & "' target='_blank'>" & strArr(iArrCnt) & "</a>")

End If

Next

Gridview1.Rows(iCnt).Cells(1).Text = strOrgText.Trim

End If

Next




Responses

Author: Curve Technologies    06 Sep 2008Member Level: Bronze   Points : 1
Instead of writing this server side code we can have a asp:template field and add a hyperlink control with in it. Provide the required url to the url property of the control and the desired result is acquired.
Thanks...


Author: Ramesh    08 Sep 2008Member Level: Silver   Points : 2
Providing a template control with a hyperlink control can be used when you know that there is only a url present in each row of the gridview.
This code can be used in scenarios where the column needs to be a bound column and need to display some message and also the url in the same cell of the gridview. Also we may not know for sure that there will be url in the message. In those situations it becomes necessary to check for the existence of a url and provide the hyperlink for the same.

Thanks,
Ramesh


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Automatic Hyperlink in Gridview Bound Column  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Bind Gridview from Session , Then Edit, Update, Delete the gridview
Previous Resource: Bind a CSV file with Gridview
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use