【问题标题】:Wrong linkbutton command argument after sorting gridview对 gridview 排序后的链接按钮命令参数错误
【发布时间】:2013-08-02 13:07:28
【问题描述】:

在 GridView 的 asp:TemplateField 列中,我有一个 LinkBut​​ton,它将命令参数传递给一个函数,该函数在单击该行时将其删除。但是,GridView 排序后,LinkBut​​ton 传递了错误的参数。此外,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);
        }
    }

【问题讨论】:

  • 您似乎没有在任何地方包含 removeCompany_click() 的代码;有可能(诚然很小)它是相关的。
  • @AdrianWragg:我编辑了问题以添加它。
  • 谢谢。排序顺序正在丢失,因为重定向清除了您存储它的 ViewState;不过,我不明白为什么发送了错误的公司 ID,这是您问题的核心。 :(
  • 如果你也输出 ; 会发生什么那是该行的正确 ID(您可以用这个在 HTML 本身中设置 CommandArgument)?另外,您在哪里进行绑定 - 是否可以在点击之前重新绑定,并选择错误的行?
  • @AdrianWragg:是的,它是正确的 ID。我编辑了问题中的代码以显示我在 Page_Load 事件中绑定了 gridview。

标签: asp.net sorting gridview linkbutton commandargument


【解决方案1】:

这就是问题所在:

当点击 LinkBut​​ton 时,首先发生的是页面重新加载。但是,Response.Redirect(Request.RawUrl) 不保留 ViewState,因此排序顺序丢失。因此,GridView 将重新填充未排序的数据。

然后,LinkBut​​ton onClick 事件函数被调用。传入的 Object 是来自正确行号的 LinkBut​​ton,但由于表的排序顺序已更改(恢复到未排序状态),该行中的 LinkBut​​ton 不再是用户单击的 LinkBut​​ton。因此,命令参数是错误的。

解决问题:

我把所有的 ViewState[string] 都改成了 Session[string] (这样在页面重新加载的时候会保留排序方向),并在绑定 GridView 之前在 Page_Load 函数中添加了如下代码:

if (Session["companiesExpression"] != null 
     && Session["companiesDirection"] != null)
{
     companies.DefaultView.Sort = Session["companiesExpression"] + " " +
          Session["companiesDirection"];
 }

【讨论】:

  • 这个解决方案对我很有效。感谢您提供解决方案。
  • 回发后控件的索引与ViewState中的DataSource匹配。出乎意料的是,CommandArgument 并不直接与 GridView 中的行相关联。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
相关资源
最近更新 更多