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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dejaanbuCommented:
post your gridview markup for checking
0
ericwong27Commented:
0
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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.