I have a gridview showing some data—naturally. One of the columns is a template field that contains a Hyperlink. As part of my rowdatabound processing, I would like to add to the querystring for that row's url. Unfortunately, I haven't been able to target the control yet.
My latest attempt is:
((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl += "&missing_info=account";
This causes a runtime error:
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.HyperLink'.
I tried targeting the TemplateControl.Controls[0] as well, but got the same result. Here is my gridview, any ideas?
<asp:GridView ID="clients_gridview" runat="server" AutoGenerateColumns="False"
DataSourceID="LinqDataSource1" AllowSorting="True"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:boundfield DataField="GroupCode" HeaderText="Group #"
SortExpression="GroupCode"></asp:boundfield>
<asp:templatefield HeaderText="SSN" SortExpression="SSN">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='test.asp?ssn=<%# Bind("SSN") %>' Text='<%# Bind("SSN") %>'></asp:HyperLink>
</ItemTemplate>
</asp:templatefield>
<asp:templatefield HeaderText="Client Name" SortExpression="LastName">
<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%# format_name(Convert.ToString(Eval("LastName")), Convert.ToString(Eval("FirstNameMI"))) %>'></asp:Label>
</itemtemplate>
</asp:templatefield>
<asp:boundfield DataField="EditDate" HeaderText="Last Updated"
SortExpression="EditDate">
</asp:boundfield>
</Columns>
</asp:GridView>