Link to home
Start Free TrialLog in
Avatar of KarenH1
KarenH1

asked on

Gridview Multiple HyperLinks in Single Cell - Part 2

I need to put multple hyperlinks in one cell in a gridview.  Each row will contain different hyperlinks - different URL's and number of links.

I have read Question ID 23165167, and am trying to use that solution, but each row is adding on to the previous row's hyperlinks.

I am trying to get one link to appear correctly in the column at this point.

Here is my code:

Protected Sub grdOfferings_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdOfferings.RowDataBound
        For Each row As GridViewRow In grdOfferings.Rows
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim hl As New HyperLink
                hl.Text = "Legal Appendix"
                hl.NavigateUrl = HTTPPATH & "/" & LEGAL_APPENDIX_FILE_NAME
                e.Row.Cells(0).Controls.Clear()
            End If
        Next
    End Sub
Avatar of alexandrupaul7
alexandrupaul7
Flag of Romania image

you could put some hyperlinks on template column, on onrowdatabound if the row is datarow find those hyperlinks with findcontrol and put them the values that you need (text, navigateurl). you could even use the visible true/false of the hyperlinks if you don't need them on some row depending of your app logic.
Avatar of KarenH1
KarenH1

ASKER

sorry, I posted the wrong code, last line before the end if is this:

e.Row.Cells(0).Controls.Add(hl)

My results are:

1st row:  "Legal Appendix"
2 row:  "Legal appendixLegalAppendix"
3 row:  "Legal appendixLegalAppendixLegal appendix"
... and so on.

I only want one "Legal Appendix" URL per row.

How can I accomplish this?
so

put a hyperlink in a template column of the gridview a hyperlink.

if you get your hyperlink values from the database use the rowdatabound event , find the hyperlink by id (e.Row.FindControl("Hyperlink1") and put the values that you want on the hyperlink.

if you want i could give you an example but only in c#
Avatar of Melih SARICA
Protected Sub grdOfferings_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdOfferings.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim hl As New HyperLink
                hl.Text = "Legal Appendix"
                hl.NavigateUrl = HTTPPATH & "/" & LEGAL_APPENDIX_FILE_NAME
                e.Row.Cells(0).Controls.Clear()
            End If
    End Sub

dont loop in grid rows..  e ll gave u the row that is binding..
Avatar of KarenH1

ASKER

Yes, please send me an example.  I'm not following how I put a hyperlink in a template column.

This is what I have added so far:

<asp:TemplateField HeaderText="Documents"></asp:TemplateField>
<asp:TemplateField HeaderText="Documents">
<asp:hyperlink runat=server id=hyp1 navigateurl='<% HTTPPATH & "/" & LEGAL_APPENDIX_FILE_NAME %>' >Legal Appendix </asp:hyperlink>
</asp:TemplateField>
Avatar of KarenH1

ASKER

Thanks, non_zero, removing the "for each row" eliminated the accumulated links in each row.

However, I am now getting an error:  "type 'system.....templatefield' does not have a public property named 'hyperlink'.

Is "hyp1" the control I will search for and put my real url in?  How will this work with a variable number of URL's?  Some rows will have 2 URL's, some 4 URL's.

Thanks so much for your help



can u post what u wrote in templatefield with template field  code
ooppss sorry

my fault...
this is the code
<asp:TemplateField HeaderText="Documents">
  <itemtemplate>
<asp:hyperlink runat=server id=hyp1 navigateurl='<% HTTPPATH & "/" & LEGAL_APPENDIX_FILE_NAME %>' >Legal Appendix </asp:hyperlink>
  </itemtemplate>
</asp:TemplateField>
Avatar of KarenH1

ASKER

One URL works, but I'm not understanding how to make this work for variable number of URL's per row.  Can you be more specific?

Also, why does the URL appear in the first column of the grid.  Can I force it to the last column?

Here is my HTML:


               
               Legal Appendix
           
               

Here is my code behind:

Protected Sub grdOfferings_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdOfferings.RowDataBound
              If e.Row.RowType = DataControlRowType.DataRow Then

            Dim hl As HyperLink = CType(e.Row.FindControl("hyp1"), HyperLink)
            hl.Text = "Legal Appendix"
            hl.NavigateUrl = HTTPPATH & LEGAL_APPENDIX_FILE_NAME

            hl.Text = hl.Text & "View Bid"
            hl.NavigateUrl = hl.NavigateUrl & HTTPPATH & "NEWONE2.PDF"
            e.Row.Cells(0).Controls.Add(hl)
           
        End If

ASKER CERTIFIED SOLUTION
Avatar of Melih SARICA
Melih SARICA
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KarenH1

ASKER

Thank you, I will use the "e.row.cells.count-1" index.  The URL is coming from another field in the database.  I will open another question for that.  Thanks for your expert help!