如何进行排序Gridview - 标题超链接(可点击)

时间:2014-05-09 19:33:10

标签: asp.net gridview hyperlink

我使用此文件作为参考 http://code.msdn.microsoft.com/VBASPNETGridView-19039173 我想按照该演示中的排序功能。 但是当我构建一个简单的页面时。我的gridview标题不是超链接

我已经有了sortExpression。但标题保留为常规文本(不可点击)。

这是我的TEST aspx的ASPX。

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplicationSandBox.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="PersonID" HeaderText="PersonID" ReadOnly="False" SortExpression="PersonID" />
                <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

这是演示文件中的ASPX文件。能帮我指点一下吗? 属性使gridview标题成为超链接(可单击)?谢谢

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DataInMemory.aspx.vb" Inherits="VBASPNETGridView.DataInMemory" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvPerson" runat="server" AutoGenerateColumns="False" BackColor="White" 
        BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            onpageindexchanging="gvPerson_PageIndexChanging" 
            onrowcancelingedit="gvPerson_RowCancelingEdit" 
            onrowdatabound="gvPerson_RowDataBound" onrowdeleting="gvPerson_RowDeleting" 
            onrowediting="gvPerson_RowEditing" onrowupdating="gvPerson_RowUpdating" 
            onsorting="gvPerson_Sorting">
        <RowStyle BackColor="White" ForeColor="#003399" />
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="PersonID" HeaderText="PersonID" ReadOnly="False" 
                    SortExpression="PersonID" />
                <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
        </asp:GridView>

        <br />
        <asp:LinkButton ID="lbtnAdd" runat="server" onclick="lbtnAdd_Click">AddNew</asp:LinkButton>
        <br />
        <br />
        <asp:Panel ID="pnlAdd" runat="server" Visible="False">
            Last name:
            <asp:TextBox ID="tbLastName" runat="server"></asp:TextBox>
            <br />
            <br />
            First name:
            <asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:LinkButton ID="lbtnSubmit" runat="server" onclick="lbtnSubmit_Click">Submit</asp:LinkButton>
            &nbsp;&nbsp;&nbsp;
            <asp:LinkButton ID="lbtnCancel" runat="server" onclick="lbtnCancel_Click">Cancel</asp:LinkButton>

        </asp:Panel>
    </div>
    </form>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

当我使用此代码时,它会将标题显示为链接 - 这是您的后续操作吗?

<asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" AllowSorting="true">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="PersonID" HeaderText="PersonID" ReadOnly="False" SortExpression="PersonID" />
            <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

答案 1 :(得分:1)

Gridview可以使用AllowSorting属性进行排序:

    <asp:GridView ID="GridView1" 
        runat="server" 
        -->AllowPaging="True" <--
        AllowSorting="True" >
        <Columns>
            {insert your columns}
        </Columns>
    </asp:GridView>

这将使您的标题可以点击。示例页面未在控件上设置AllowSorting。页面后面的代码显示了他们在代码隐藏中设置的位置:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
' The Page is accessed for the first time. 
If Not IsPostBack Then 
    ' Enable the GridView paging option and  
    ' specify the page size. 
    gvPerson.AllowPaging = True 
    gvPerson.PageSize = 15 


    ' Enable the GridView sorting option. 
-->        gvPerson.AllowSorting = True <--


    ' Initialize the sorting expression. 
    ViewState("SortExpression") = "PersonID ASC" 


    ' Populate the GridView. 
    BindGridView() 
End If 
End Sub 

希望有所帮助。 :)