我有一个GridView,我正在尝试排序,并且排序有效,但是出于某种原因,第一次点击并没有开始排序。我需要在排序开始之前两次单击标题列中的排序字段。希望有人能够识别我的问题:
ASPX代码:
<asp:GridView ID="GridView_PendingCounts" runat="server" AllowPaging="True"
AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" ShowHeader="True"
style="margin-right: 0px" PageSize="50"
OnSorting="GridView_PendingCounts_Sorting"
OnPageIndexChanging="GridView_PendingCounts_PageIndexChanging" SortedAscendingHeaderStyle="true"
SortedDescendingHeaderStyle="true">
cs code:
点击处理程序:
protected void GridView_PendingCounts_Sorting(object sender, GridViewSortEventArgs e)
{
//Create/update cookie to sort data on sproc pull in GemericRules - spTeamComplianceGetCountsSearch
//Cookie string is stored as : Cookie name: Table (TCSortGridViewPending = PendingCounts, TCSortGridViewApproved = ApprovedCounts, TCSortGridViewTotal = TotalCounts)
//Cookie value: Sort Expression (SQLDB Table column header), Sort Direction ("ASC"/"DESC")
//note that e.SortDirection is not persisting with postback. To get around this i had to simply check for ASC in string, and if exists switch to DESC.
var sortDirection = " ASC";
if (!string.IsNullOrEmpty(Cookies_Get("TCSortGridViewPending")) && Cookies_Get("TCSortGridViewPending").IndexOf(" ASC", StringComparison.Ordinal) > 0)
{
sortDirection = " DESC";
}
//Kill previous cookie value
Cookies_Delete("TCSortGridViewPending");
Cookies_Set("TCSortGridViewPending", SortExpressionAdapter(e.SortExpression) + sortDirection, DateTime.Now.AddHours(24));
//Cookie Debug alert:
//ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Sorting click:" + Cookies_Get("TCSortGridViewPending") + "');", true);
GenericRules();
//Per Joel, if we sort a column, we want to switch pagination back to origin. We do this by settting page index to 0
GridView_PendingCounts.PageIndex = 0;
GridView_PendingCounts.DataBind();
}
最后,剪切通过dataview应用排序的通用规则,通过指向SPROC返回的数据集中的表来实例化:
//now that we have our data set populated, we need to issue sorting on tables tblTotal, tblApproved, and tblPending
//based upon the cookies set in the Sorting controls.
//Cookie string is stored as : Cookie name: Table (TCSortGridViewPending = PendingCounts, TCSortGridViewApproved = ApprovedCounts, TCSortGridViewTotal = TotalCounts)
//Cookie value: Sort Expression (SQLDB Table column header), Sort Direction ("ASC"/"DESC")
//First check if the cookie exists, and if so, apply sorting to the designated table.
//Cookies:
// TCSortGridViewTotal - contains: SortExpression(SQLDB table column header),ASC/DESC
// TCSortGridViewApproved - contains: SortExpression(SQLDB table column header),ASC/DESC
// TCSortGridViewPending - contains: SortExpression(SQLDB table column header),ASC/DESC
var vwTotal = new DataView();
var vwApproved = new DataView();
var vwPending = new DataView();
//Here we create views for each table so we can apply sorting if the cookie exists to apply sorting rules.
if (ds.Tables["tblTotal"] != null && ds.Tables["tblTotal"].Rows.Count > 0)
{
vwTotal = ds.Tables["tblTotal"].DefaultView;
}
if (ds.Tables["tblApproved"] != null && ds.Tables["tblApproved"].Rows.Count > 0)
{
vwApproved = ds.Tables["tblApproved"].DefaultView;
}
if (ds.Tables["tblPending"] != null && ds.Tables["tblPending"].Rows.Count > 0)
{
vwPending = ds.Tables["tblPending"].DefaultView;
}
if (!string.IsNullOrEmpty(Cookies_Get("TCSortGridViewTotal")))
{
//Debug cookie value:
//ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Cookies_Get("TCSortGridViewTotal") + "');", true);
vwTotal.Sort = Cookies_Get("TCSortGridViewTotal");
}
if (!string.IsNullOrEmpty(Cookies_Get("TCSortGridViewApproved")))
{
//Debug cookie value:
//ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Cookies_Get("TCSortGridViewApproved") + "');", true);
ds.Tables["tblApproved"].DefaultView.Sort = Cookies_Get("TCSortGridViewApproved");
}
if (!string.IsNullOrEmpty(Cookies_Get("TCSortGridViewPending")))
{
//Debug cookie value:
//ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Cookies_Get("TCSortGridViewPending") + "');", true);
ds.Tables["tblPending"].DefaultView.Sort = Cookies_Get("TCSortGridViewPending");
}
排序工作正常,它只是不适用我的第一次点击。我甚至试图在设置后续排序点击事件之前删除cookie,但无济于事。知道为什么我的排序onclick事件似乎在回发/重绘之前没有被击中?
我还注意到,如果我点击刷新,排序就会开始...所以这证明了排序DOES的onclick应用了cookie,但由于某些原因它直到我执行额外的刷新才会呈现。
非常感谢帮助。我在这里没有想法。