Add to Hyperlink Grid View cell programatically

I have a databound grid view to which I added a hyperlink field to from the Fields dialog on the first column.

For each row I want to run some SQL that will return a count of records and add it to the end of my hyperlink field text so this can be seen from the grid view without having to go into the page via the hyperlink.

I have attached some test code I was trying to get working but this replaces my hyperlink field instead of adding to it (it seems to add correctly to the abound fields).

My other problem is, instead of hard coding "(0)" I need to pickup the EventID from the grid view to run my SQL that gives me my count but cant seem to get it possibly becaus it's visible property is set to false?  I think using the code below I can only pickup post rendered columns.

Any help would be greatfully recieved!
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(0).Text &= " (0)"
        End If
    End Sub

Open in new window

LVL 8
Alex_WAsked:
Who is Participating?
 
dejaanbuCommented:
I missed ur ID. updating again..
ur gridview markup should be

<asp:TemplateField>
                <ItemTemplate>
<asp:Label ID="theID" runat="server" Style="display:none" Text='<%#Eval("EventId")%>'></asp:Label>
                    <asp:HyperLink ID="HyperLink1" runat="server" 
                        NavigateUrl='<%# Eval("EventId", "~/Members/SelectEvent.aspx?EventId={0}") %>' 
                        Text="See Details"></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>

Open in new window


now in Gridview rowdatabound

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
       Dim hLink As HyperLink = DirectCast(e.Row.FindControl("HyperLink1"),HyperLink)
dim theId as Label = DirectCast(e.Row.FindControl("theID"),Label)

      hLink.Text ="See Details " & theId.Text
End If

End sub

Open in new window


0
 
dejaanbuCommented:
post your gridview markup for checking
0
 
ericwong27Commented:
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Alex_WAuthor Commented:
Here is the markup for my hyperlink field:

           
<asp:TemplateField>
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" 
                        NavigateUrl='<%# Eval("EventId", "~/Members/SelectEvent.aspx?EventId={0}") %>' 
                        Text="See Details"></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>

Open in new window


It was a HyperLinkField but I converted to template field to get an ID for it.  On the code side I have tried things like:

Dim hLink As HyperLink = CType(e.Row.Cells(0).Controls(0), HyperLink)
hLink.Text &= "(0)"

Open in new window


- but the control doesnt get passed to the code.  This sounds like it should be much simpler, I just want to add text to the text property of the hyperlink at runtime.
0
 
dejaanbuCommented:
Try this

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
       Dim hLink As HyperLink = DirectCast(e.Row.FindControl("HyperLink1"),HyperLink)
      hLink.Text ="Your Text"
End If

End sub

Open in new window

0
 
CodeCruiserCommented:
Why not retrieve the count as part of your SQL statement and then just set the text in same way that you are setting the navigate url.
0
 
Alex_WAuthor Commented:
I have nearly got it working from the posted code but I can't seem to do this when converting the HyperLinkField to a TemplateField when I have more than one DataNavigateUrlFields.

Here is my Hyperlinkfield:

<asp:HyperLinkField DataNavigateUrlFields="EventID,AlertedEventID"
                    DataNavigateUrlFormatString="~/Members/SelectEventJoined.aspx?EventId={0}&amp;AlertedEventID={1}"
                    Text="See Details">
                <ItemStyle HorizontalAlign="Center" />
                </asp:HyperLinkField>

- And here it is after converting to a template field:

<asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server"
                            NavigateUrl='<%# Eval("EventID", "~/Members/SelectEventJoined.aspx?EventId={0}&AlertedEventID={1}") %>'
                            Text="See Details"></asp:HyperLink>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>



- it just doesnt like my second NavigateUrlField, I tried adding it with EventID but this didnt work:

NavigateUrl='<%# Eval("EventID,AlertedEventID", .....

Could I leave it as a HyperLinkField and still do this?
0
 
CodeCruiserCommented:
You can use HyperLinkField  or you can change

 <asp:HyperLink ID="HyperLink1" runat="server"
                            NavigateUrl='<%# Eval("EventID", "~/Members/SelectEventJoined.aspx?EventId={0}&AlertedEventID={1}") %>'

to

 <asp:HyperLink ID="HyperLink1" runat="server"
                            NavigateUrl='<%# String.Format("~/Members/SelectEventJoined.aspx?EventId={0}&AlertedEventID={1}", Eval("EventID", Eval("AlertedEventID")) %>'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.