3

我很难将排序箭头隐藏在不可排序的列标题上。我在 HeaderTemplate 部分中明确定义了可排序的列,在 ItemTemplate 中定义了不可排序的列,但对于下面定义的第二个 TemplateField,排序箭头仍然出现。我错过了什么?

        <asp:GridView ID="gvBeneficiary" runat="server" Width="100%" AllowPaging="True" CssClass="gridheader"
            EmptyDataText=""
            AutoGenerateColumns="False" PageSize="10"
            OnPageIndexChanging="gvBeneficiary_PageIndexChanging" OnRowCommand="gvBeneficiary_RowCommand"
            OnRowDataBound="gvBeneficiary_RowDataBound" HeaderStyle-CssClass="lhs">
            <Columns>
                <asp:TemplateField HeaderStyle-CssClass="lhs">
                    <HeaderTemplate>
                        <asp:LinkButton ID="lnkFullName" Text="Full Name"
                            CommandName="Sort" CommandArgument="FullName" runat="server"></asp:LinkButton>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%#Eval("FullName") %>
                    </ItemTemplate>
                </asp:TemplateField>

               <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkEdit" runat="server" CommandName="EditClick" CommandArgument='<%#Eval("RecipientID") %>'
                            Text="Edit">
                        </asp:LinkButton>
                        <asp:LinkButton ID="lnkDelete" runat="server" CommandName="DeleteClick" CommandArgument='<%#Eval("RecipientID") %>'
                            Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

4

4 回答 4

3

在 ItemTemplate 中定义不可排序的列是什么意思?为您希望启用排序的各个列的网格 asd 排序表达式指定 Allowsorting =true。

<asp:GridView AllowSorting="true"......../> and 
  <asp:TemplateField..............  SortExpression ="Full Name"> 

并且不要为不需要排序的列指定排序表达式。

我认为这足以对指定的列进行排序。

于 2012-08-17T06:40:54.973 回答
0

我已使用 listview 完成此操作。请尝试使用 Gridview

我的html代码是

<asp:ListView ID="lst_Area" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_Area_ItemDataBound">
                            <LayoutTemplate>
                                <table cellspacing="0">
                                    <tr class="hdrRowColor1">
                                        <td width="35px" align="left">
                                            &nbsp;S.No
                                        </td>
                                        <td align="left" width="400px">
                                            <asp:LinkButton ID="lnk_Name" runat="server" CommandArgument="tblAreaNew.AreaName"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Name</asp:LinkButton>
                                            <asp:Image ID="img_lnk_Name" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="250px">
                                            <asp:LinkButton ID="lnk_Location" runat="server" CommandArgument="tblAreaNew.Locationid"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Location</asp:LinkButton>
                                            <asp:Image ID="img_lnk_Location" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="175px">
                                            <asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="v.FirstName" ValidationGroup="vgSearch"
                                                OnClick="lnk_Sort">Created By</asp:LinkButton>
                                            <asp:Image ID="img_lnk_CreatedBy" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="120px">
                                            <asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblAreaNew.createddate"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
                                            <asp:Image ID="img_lnk_CreatedOn" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="175px">
                                            <%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
                                        OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
                                            <asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified By</asp:LinkButton>
                                            <asp:Image ID="img_lnk_LastModfiedBy" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="120px">
                                            <asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblAreaNew.ModifiedDate"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
                                            <asp:Image ID="img_lnk_LastModfiedOn" Visible="false" runat="server" />
                                        </td>
                                        <td align="center" width="50px">
                                            <asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblAreaNew.isactive"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
                                            <asp:Image ID="img_lnk_Status" Visible="false" runat="server" />
                                        </td>
                                        <td align="center" width="50px" style="border-right: 1px solid #6398cc;">
                                            Activity
                                            <%-- <div style="width: 50px; float: right;">
                                            </div>--%>
                                        </td>
                                    </tr>
                                    <tr id="tr" runat="server">
                                    </tr>
                                </table>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td align="left" valign="middle">
                                        &nbsp;<%# Container.DataItemIndex+1 %>.
                                    </td>
                                    <td align="left">
                                        <asp:Label ID="lblAreaID" runat="server" Text='<%# Eval("Areaid")%>' Visible="false"></asp:Label>
                                        <%# Eval("AreaName")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("Location")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("CreatedBy")%>
                                    </td>
                                    <td align="left">
                                        <%# Convert.ToDateTime(Eval("CreatedDate")).ToString("MMM, dd yyyy")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("ModifiedBy")%>
                                    </td>
                                    <td align="left">
                                        <%# Convert.ToDateTime(Eval("ModifiedDate")).ToString("MMM, dd yyyy")%>
                                    </td>
                                    <td align="center">
                                        <asp:Label ID="lblLocationIsActive" runat="server" Style="display: none;" Text='<%# Eval("LocationIsActive")%>'></asp:Label>
                                        <asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
                                        <asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("Areaid") %>'
                                            OnClick="imgbtnStatus_Onclick" />
                                    </td>
                                    <td class="last" align="center">
                                        <asp:Label ID="lblAreaName" runat="server" Style="display: none;" Text='<%# Eval("AreaName")%>'></asp:Label>
                                        <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
                                            ToolTip="Edit Details" CommandArgument='<%# Eval("AreaId") %>' OnClick="imgbtnEdit_OnClick" />
                                        <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
                                            ToolTip="Delete" CommandArgument='<%# Eval("AreaId") %>' Visible="false" OnClientClick="return confirm('Are you sure you want to delete the location?');"
                                            OnClick="imgbtnDelete_OnClick" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:ListView>

我的代码背后的代码是

 protected void lnk_Sort(object sender, EventArgs e)
{
    LinkButton lnk = (LinkButton)sender;
    string arg = lnk.CommandArgument.ToString();
    ViewState["sortCol"] = arg;
    GetSortDirection();
    BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
    string name = lnk.ID;
    Image img = (Image)(lst_Area.FindControl("img_" + name));
    if (img != null)
    {
        SetSortOrderImage(img, ViewState["sortDir"].ToString());
    }
}

private void SetSortOrderImage(Image image, String sortorder)
{
    if (sortorder == "asc")
    {
        image.Visible = true;
        image.ImageUrl = "../App_Themes/ThemeNew2/images/up.png";
    }
    else if (sortorder == "Desc")
    {
        image.Visible = true;
        image.ImageUrl = "../App_Themes/ThemeNew2/images/down.png";

    }
}

/// <summary>
/// this method get the sort direction
/// </summary>
private void GetSortDirection()
{
    if (Convert.ToString(ViewState["sortDir"]) == "Desc")
    {
        ViewState["sortDir"] = "asc";
    }
    else
    {
        ViewState["sortDir"] = "Desc";
    }
}

希望这对你有用。我想让你知道,与自定义分页和排序相比,gridview 的内置排序和分页速度较慢。

于 2012-08-21T13:49:20.560 回答
0

您是否有理由不能在 gridview 中使用默认排序功能?如果可以,将“箭头”CSS 应用于表格标题单元格中的所有超链接

.gvclass th a {background-image...}

这样,所有带有 SortExpression 的标头都将具有这些 CSS 属性,而所有不可排序的标头都将没有(因为在这些单元格中不会生成超链接)。

于 2012-08-16T06:28:51.020 回答
0

如果您只想禁用事件中特定列的排序,通常应该将GridViewColumn's设置SortExpressionnull

gvBeneficiary.Columns[0].SortExpression = null;
于 2012-08-21T06:12:34.270 回答