开发者

How do I make the gridview headers to links?

I want the headers of my gridview to be hyperlinks, without the 开发者_开发百科"SortExpression"... I searched the net, but I've been not very succesful.

Anyone has the solution?

For example: when clicking on the header of a simple gridview, the site navigates to a webpage. Is it possible?

Thanks in advance!


Have you tried Gridview Header template like...

 <asp:GridView runat="server" ID="grd">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:HyperLink runat="server" NavigateUrl="YourURL"> </asp:HyperLink>
                </HeaderTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


I think a HeaderTemplate is needed here...

Ref.: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.headertemplate.aspx

HTH.


Now I got this gridview, And I need the headers to be clickable, whereafter an event starts (something like OnClickHeader="header_ClickEvent"?) Ofcourse there is a SortExpression element, which enables to sort the grid, but I want to be able to start any event, like when clicking a button.

I could not find any solution within the asp:BoundField nor asp:TemplateField... I thought a hyperlink could solve the problem, but that was a bit premature.

The Gridview:

<asp:GridView CssClass="gridview" ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Student_key" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PagerSettings-Visible="false" PageSize="14">

                                    <HeaderStyle CssClass="headerstyle" />
                                    <RowStyle CssClass="rowstyle"/>
                                    <AlternatingRowStyle CssClass="altrowstyle" />
                                    <Columns>
                                        <asp:BoundField DataField="Studentnumber" HeaderText="Studentnummer" >
                                            <HeaderStyle CssClass="header100" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Prefix" HeaderText="Voorletters" >
                                            <HeaderStyle CssClass="header75" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="prename" HeaderText="Voornaam" SortExpression="Voornaam">
                                            <HeaderStyle CssClass="header75" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="nickname" HeaderText="Roepnaam" >
                                            <HeaderStyle CssClass="header100" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="insertion" HeaderText="Tussenvoegsel" >
                                            <HeaderStyle CssClass="header100" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="surname" HeaderText="Achternaam">
                                            <HeaderStyle CssClass="header100" />
                                        </asp:BoundField>
                                        <asp:CommandField SelectText="show results" ShowSelectButton="True" >
                                            <HeaderStyle CssClass="header100" />
                                        </asp:CommandField>

                                    </Columns>
                                    <EmptyDataTemplate >There are no results shown, please try again.</EmptyDataTemplate>

                                </asp:GridView>


I used a method that might be a little unconventional but it works. In my case I wanted to use the standard BoundField controls in my gridview as opposed to using a template field with both a HeaderTemplate and ItemTemplate. I used a simple gridview based on a SQL datasource that looks like this.

<asp:GridView
    ID="gvTopXByContest"
    runat="server"
    AutoGenerateColumns="False"
    DataSourceID="dsTopXByContest"
    AllowSorting="true" 
    OnSorting="gvTopXByContest_OnSorting" >
    <Columns>
        <asp:BoundField DataField="txtOnlineUserName" HeaderText="Fan Name & Rank"                   SortExpression="txtOnlineUserName" ItemStyle-Width="155px"></asp:BoundField>
        <asp:BoundField DataField="fltTotalPoints" HeaderText="Points" SortExpression="fltTotalPoints" ItemStyle-Width="40px"></asp:BoundField>
        <asp:BoundField DataField="curWon" HeaderText="Won" SortExpression="curWon" ItemStyle-Width="40px"></asp:BoundField> 
</Columns>
</asp:GridView>

I then used code that fires on the OnSorting event of the gridview to do my redirects

Protected Sub gvTopXByContest_OnSorting(sender As Object, e As GridViewSortEventArgs)

    If e.SortExpression <> DirectCast(sender, GridView).SortExpression Then
        If e.SortExpression = "txtOnlineUserName" Then
            Response.Redirect(URL to redirect to goes here)
        ElseIf e.SortExpression = "fltTotalPoints" Then
            Response.Redirect(URL to redirect to goes here)
        Else
            'I could have used another ElseIf here but since there are only 3 columns Else works
            Response.Redirect(URL to redirect to goes here)
        End If

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜