Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting links into a gridview dynamically (.net 4.5.1)

Status
Not open for further replies.

mdProgrammer

Programmer
Oct 2, 2004
71
0
0
US
I have an application which was written in .NET 1.1 that used a datagrid that I would dynamically change a cell into a link based on the column and security settings. For example:

Column is an ID field and user has access to the record - Show a link.
Column is an ID field, and user does not have access to the recod - Don't show the link (normal text).
Column is not an ID field - Don't show the link (normal text).

I'm attempting to create a user control for .NET 4.5 (First major project using 4.5, and there's definately a lot of different, and nice features added, so it's a steep learning curve).

I've read that gridviews are the successors to datagrids, and I'm trying to create a usercontrol based on the gridview. Anyway, my problem is that when I change the text to a link in a gridview, it displays the HTML code (the HTML source shows <, &rt;, etc.). I even tried &rt;, < in the code, and it didn't work. However, with the datagrid, links would work just fine. (It's not just links, but any HTML code, like making text bold) Is there a way to make the HTML show up in an individual cell?

I know there are linkcolumns that I can add, but the ID field isn't always known, if it exists at all (as the control is used in multiple webforms).

This is some test code -

Code:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DataGrids.aspx.vb" Inherits="WebApplication1.DataGrids" %>

<!DOCTYPE html>

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />

      <br />
      <strong>DataGrid</strong><asp:datagrid id="dgDataGrid" runat="server">
</asp:datagrid>

      <br />
      <strong>GridView</strong><asp:GridView ID="gvGridView" runat="server">
      </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Code:
Public Class DataGrids
    Inherits System.Web.UI.Page

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

    End Sub

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim db As New dbData  ' Temporary class name from old application with hideiously non-refactored code. :)

    Dim dt As DataTable = db.getDataTable("SELECT 'Test' as 'ID_Column'", db.InventoryConnection)

    Dim dv As New DataView(dt)

    For i As Integer = 0 To dv.Table.Rows.Count - 1

      If dv.Table.Columns(0).ColumnName = "ID_Column" Then
        dv.Table.Rows(i).Item(0) = "<a href='[URL unfurl="true"]http://www.google.com'[/URL] target='_blank'>Google</a>"
      End If

    Next

    dgDataGrid.DataSource = dv
    gvGridView.DataSource = dv

    dgDataGrid.DataBind()
    gvGridView.DataBind()

  End Sub

End Class
 
IF you don't want the actual HTML markup to show, you have to HTMLEncode() the text.
 
I did manage to find a work-around for what I want, via modifying the gridview after it has been databound. I also found and slightly modified some code I found on the Internet (the original assumed that the column would be found, so I added a boolean to check if that was the case or not. If not, return "Nothing").

Code:
.
.
.
    For i As Integer = 0 To gvGridView.Rows.Count - 1
      Dim id As String = GetColumnIndexByName(gvGridView.Rows(i), "ID_Column")

      If Not id = Nothing Then
        gvGridView.Rows(i).Cells(id).Text = "<a href='[URL unfurl="true"]http://www.google.com'[/URL] target='_blank'>Google</a>"

      End If
    Next

  End Sub

  Function GetColumnIndexByName(row As GridViewRow, columnName As String) As Object

    Dim columnIndex As Integer = 0
    Dim found As Boolean = False

    For Each cell As DataControlFieldCell In row.Cells


      If (cell.ContainingField.HeaderText = columnName) Then
        found = True
        Exit For
      End If
      columnIndex += 1 ' keep adding 1 while we don't have the correct name

    Next

    If found Then
      Return columnIndex
    End If
    Return Nothing
  End Function
 
To add, I'm not sure if it's the best practice, but it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top