0
你好朋友我的分頁工作正常,但是當排序任何列時,它會在第一頁排序,但在下一頁排序會丟失。排序不能在asp.net的網格視圖中使用分頁
請任何人都可以幫我弄清楚這一點。
我的標記代碼的.aspx
<asp:TemplateField HeaderText="Actions" ItemStyle-Width="20%">
<ItemTemplate>
<a href='#' onclick="javascript:view_data(<%#Eval("Substance_ID")%>)"><span class="glyphicon glyphicon-eye-open"></span>View</a>
<a href='edit.aspx?id=<%#Eval("Substance_ID")%>'><span class="glyphicon glyphicon-pencil"></span>Edit</a>
<a href='delete.aspx?id=<%#Eval("Substance_ID")%>'><span class="glyphicon glyphicon-trash"></span>Delete</a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Substance_ID" HeaderText="Substance ID" SortExpression="Substance_ID" />
<asp:TemplateField HeaderText="CAS Number" ItemStyle-Width="10%" SortExpression="CAS_Number">
<ItemTemplate>
<%# Eval("CAS_Number").ToString().Trim()%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EC Number" ItemStyle-Width="10%" SortExpression="EC_Number">
<ItemTemplate>
<%# Eval("EC_Number").ToString().Trim()%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Substance Name" SortExpression="Substance_Name">
<ItemTemplate>
<%#Eval("Substance_Name").ToString().Trim()%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Primary SG ID" ItemStyle-Width="10%" SortExpression="Primary_SG_ID">
<ItemTemplate>
<%#Eval("Primary_SG_ID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" ItemStyle-Width="5%" SortExpression="Status">
<ItemTemplate>
<%#Eval("Status")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="my_pagination" />
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" />
<EmptyDataTemplate>No Data</EmptyDataTemplate>
</asp:GridView>
<asp:Label runat="server" ID="lblCount"></asp:Label>
後面我的代碼:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGridViewData();
}
}
private void BindGridViewData()
{
List<Substance_Display> Substance_List = new List<Substance_Display>();
Substance_List = Substance_MasterAccessLayer.GetAllSubstances("Substance_ID");
Substance_Master.DataSource = Substance_List;
Substance_Master.DataBind();
lblCount.Text = "Total " + Substance_List.Count().ToString() + " Records found.";
}
protected void Substance_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Substance_Master.PageIndex = e.NewPageIndex;
Substance_Master.DataBind();
}
private void SortGridView(GridView Substance_Master, GridViewSortEventArgs e, out SortDirection sortDirection, out string sortField)
{
sortField = e.SortExpression;
sortDirection = e.SortDirection;
if (Substance_Master.Attributes["CurrentSortField"] != null & Substance_Master.Attributes["CurrentSortDirection"] != null)
{
if (sortField == Substance_Master.Attributes["CurrentSortField"])
{
if (Substance_Master.Attributes["CurrentSortDirection"] == "ASC")
{
sortDirection = SortDirection.Descending;
}
else
{
sortDirection = SortDirection.Ascending;
}
}
Substance_Master.Attributes["CurrentSortField"] = sortField;
Substance_Master.Attributes["CurrentSortDirection"] = (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
}
}
protected void Substance_Master_Sorting(object sender, GridViewSortEventArgs e)
{
SortDirection sortDirection = SortDirection.Ascending;
string sortField = string.Empty;
SortGridView(Substance_Master, e, out sortDirection, out sortField);
string strSortDirection = sortDirection == SortDirection.Ascending ? "ASC" : "DESC";
Substance_Master.DataSource = Substance_MasterAccessLayer.GetAllSubstances(e.SortExpression + " " + strSortDirection);
Substance_Master.DataBind();
}
我試過使用,但它仍然不工作,同樣的問題仍然存在,任何其他線索 –
@Sagar Mahajan - 我編輯了我的答案。 – afzalulh
非常感謝解決方案,問題已得到解決。 –