我已使用 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">
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">
<%# 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 的内置排序和分页速度较慢。