【问题标题】:How to make Sorting Gridview - header a hyperlink (clickable)如何使排序 Gridview - 标题成为超链接(可点击)
【发布时间】:2014-05-09 19:33:09
【问题描述】:

我使用这个文件作为参考 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>

【问题讨论】:

  • 向建议 AllowSorting 的用户道歉。那是正确的
  • 不用担心。我删除了我的评论并将其作为答案。很高兴你修好了。

标签: asp.net gridview hyperlink


【解决方案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>

【讨论】:

  • 演示代码没有显示allowSorting。但它确实有效。那让我失望了。谢谢
【解决方案2】:

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 

希望对您有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 2014-04-08
    • 2014-10-06
    • 2010-10-26
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多