Changing NavigateURL RowDatabound event

Last post 08-15-2007 4:48 PM by treybean. 2 replies.

Sort Posts:

  • Changing NavigateURL RowDatabound event

    08-14-2007, 7:33 PM
    • Loading...
    • treybean
    • Joined on 08-14-2007, 7:21 PM
    • Posts 6

    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>


     

  • Re: Changing NavigateURL RowDatabound event

    08-14-2007, 11:36 PM
    Answer
    • Loading...
    • ghinx
    • Joined on 03-23-2007, 2:55 AM
    • Sydney
    • Posts 203

    I think you need to do something like this

    HyperLink hl = (HyperLink)e.Row.FindControl("nameofhyperlink");

    hl.NavigateUrl +=  "&missing_info=account";

  • Re: Changing NavigateURL RowDatabound event

    08-15-2007, 4:48 PM
    • Loading...
    • treybean
    • Joined on 08-14-2007, 7:21 PM
    • Posts 6

     Thanks ghinx!

     I ended up converting that column to a HyperLinkField and was then able to go straight to the controls collection like:

    ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl += "&missing_info=account";

    I did verify that your suggestion did work, though. 
     

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter