在GridView的asp:TemplateField列中,我有一个LinkButton,它将一个命令参数传递给一个函数,该函数在单击时删除该行。但是,在对GridView进行排序后,LinkButton会传递错误的参数。此外,GridView丢失了排序顺序。
我做错了什么?
这是我的代码:
<!---master page---->
<asp:GridView runat="server" ID="companies_grid" AllowSorting="true"
AutoGenerateColumns="false" OnSorting="companies_grid_OnSorting"
OnRowDataBound="companies_grid_OnRowDataBound" >
<Columns>
<%--Company Name--%>
<asp:TemplateField HeaderText="Company Name" SortExpression="Name">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
OnClick="removeCompany_click" />
<a href='<%#Eval("URL")%>'><%#Eval("Name")%></a>
</ItemTemplate>
</asp:TemplateField>
//more columns
<!---code behind---->
protected void Page_Load(object sender, EventArgs e)
{
companies = GetCompanyData();
companies_grid.DataSource = companies;
companies_grid.DataBind();
}
protected void companies_grid_OnSorting(object sender, GridViewSortEventArgs e)
{
//sort is made up of column to sort by + direction
companies.DefaultView.Sort = e.SortExpression.ToString() + " " + GetSortDirection(e.SortExpression, "companiesExpression", "companiesDirection");
companies_grid.DataSource = companies;
companies_grid.DataBind();
}
private string GetSortDirection(string column, string expressionViewState, string directionViewState)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState[expressionViewState] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState[directionViewState] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState[directionViewState] = sortDirection;
ViewState[expressionViewState] = column;
return sortDirection;
}
protected void companies_grid_OnRowDataBound(Object Sender, GridViewRowEventArgs e)
{
GridViewRow currRow = e.Row;
if (currRow.RowType == DataControlRowType.DataRow)
{
LinkButton deleteCompButton = (LinkButton)e.Row.FindControl("LinkButton1") as LinkButton;
deleteCompButton.CommandArgument = ((DataRowView)e.Row.DataItem)["Company_ID"].ToString();
deleteCompButton.Text = ((DataRowView)e.Row.DataItem)["Company_ID"].ToString();
}
}
protected void removeCompany_click(Object sender, EventArgs e)
{
bool removeSuccess = false;
string idToDelete = ((LinkButton)sender).CommandArgument as string;
removeSuccess = UserInfo.DeleteCompany(idToDelete);
if (removeSuccess)
{
Response.Redirect(Request.RawUrl);
}
}
答案 0 :(得分:2)
问题在于:
单击LinkButton时,首先发生的是页面重新加载。但是,Response.Redirect(Request.RawUrl)
不保留ViewState,因此排序顺序丢失。因此,GridView将使用未排序的数据重新填充。
然后,调用LinkButton onClick事件函数。传入的Object是来自正确行号的LinkButton,但由于表的排序顺序已更改(返回其未排序状态),该行中的LinkButton不再是用户单击的LinkButton。因此,命令参数是错误的。
解决问题:
我将所有ViewState [string]更改为Session [string](以便在页面重新加载时保留排序方向),并在GridView绑定之前在Page_Load函数中添加以下代码:
if (Session["companiesExpression"] != null
&& Session["companiesDirection"] != null)
{
companies.DefaultView.Sort = Session["companiesExpression"] + " " +
Session["companiesDirection"];
}