4

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);
        }
    }
4

1 に答える 1

2

ここに問題がありました:

LinkBut​​ton がクリックされると、最初にページがリロードされます。ただし、Response.Redirect(Request.RawUrl)ViewState は保持されないため、並べ替え順序が失われます。したがって、GridView には並べ替えられていないデータが再設定されます。

次に、LinkBut​​ton onClick イベント関数が呼び出されます。渡されたオブジェクトは正しい行番号の 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"];
 }
于 2013-08-01T17:21:28.943 に答える