Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 对gridview排序后出现错误的linkbutton命令参数_Asp.net_Sorting_Gridview_Linkbutton_Commandargument - Fatal编程技术网

Asp.net 对gridview排序后出现错误的linkbutton命令参数

Asp.net 对gridview排序后出现错误的linkbutton命令参数,asp.net,sorting,gridview,linkbutton,commandargument,Asp.net,Sorting,Gridview,Linkbutton,Commandargument,在GridView的asp:TemplateField列中,我有一个LinkButton,它将命令参数传递给一个函数,该函数在单击行时删除该行。但是,GridView排序后,LinkButton传递了错误的参数。此外,GridView会丢失排序顺序 我做错了什么 这是我的密码: <!---master page----> <asp:GridView runat="server" ID="companies_grid" AllowSorting="true" AutoGe

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

//更多栏目
受保护的无效页面加载(对象发送方、事件参数e)
{
companys=GetCompanyData();
companies_grid.DataSource=公司;
公司_grid.DataBind();
}
受保护的无效公司\u网格\u排序(对象发送方、GridViewSortEventArgs e)
{
//排序由按+方向排序的列组成
companys.DefaultView.Sort=e.SortExpression.ToString()+“”+GetSortDirection(e.SortExpression,“companyesexpression”,“companyesdirection”);
companies_grid.DataSource=公司;
公司_grid.DataBind();
}
私有字符串GetSortDirection(字符串列、字符串表达式ViewState、字符串方向ViewState)
{
//默认情况下,将排序方向设置为升序。
字符串sortDirection=“ASC”;
//检索已排序的最后一列。
string-sortExpression=ViewState[expressionViewState]作为字符串;
if(sortExpression!=null)
{
//检查是否对同一列进行排序。
//否则,可以返回默认值。
if(sortExpression==列)
{
字符串lastDirection=ViewState[directionViewState]作为字符串;
如果((lastDirection!=null)&&(lastDirection==ASC”))
{
sortDirection=“DESC”;
}
}
}
//在ViewState中保存新值。
ViewState[directionViewState]=sortDirection;
ViewState[expressionViewState]=列;
返回排序方向;
}
受保护的无效公司\u网格\u OnRowDataBound(对象发送方,GridViewRowEventArgs e)
{
GridViewRow currRow=e.行;
if(currRow.RowType==DataControlRowType.DataRow)
{
LinkButton deleteCompButton=(LinkButton)e.Row.FindControl(“LinkButton1”)作为LinkButton;
deleteCompButton.CommandArgument=((DataRowView)e.Row.DataItem)[“公司ID”].ToString();
deleteCompButton.Text=((DataRowView)e.Row.DataItem)[“公司ID”].ToString();
}
}
受保护的无效删除公司单击(对象发送方,事件参数e)
{
bool removeSuccess=false;
字符串idToDelete=((LinkButton)sender).CommandArgument作为字符串;
removeSuccess=UserInfo.DeleteCompany(idToDelete);
如果(移除成功)
{
重定向(Request.RawUrl);
}
}
问题出在这里:

单击LinkButton时,发生的第一件事是重新加载页面。但是,
Response.Redirect(Request.RawUrl)
不保留视图状态,因此排序顺序丢失。因此,GridView将重新填充未排序的数据

然后,调用LinkButton onClick事件函数。传入的对象是正确行号中的LinkButton,但由于表的排序顺序已更改(返回到未排序状态),该行中的LinkButton不再是用户单击的LinkButton。因此,命令参数是错误的

要解决此问题,请执行以下操作:

我将所有ViewState[string]更改为Session[string](以便在页面重新加载时保留排序方向),并在绑定GridView之前在page_Load函数中添加了以下代码:

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

您似乎没有在任何地方包含removeCompany\u click()的代码;有一个机会(诚然很小)是相关的。@AdrianWragg:我编辑了这个问题来添加它。谢谢。排序顺序丢失,因为重定向清除了存储它的ViewState;不过,我不明白为什么发送了错误的公司ID,这是你问题的核心(如果同时输出,会发生什么情况;这是行的正确ID吗(您可以在HTML本身中使用此设置CommandArgument)?另外,您在哪里进行绑定-是否在按下单击之前重新绑定,并拾取错误的行?@AdrianWragg:是的,它是正确的ID。我编辑了问题中的代码,以显示我正在页面加载事件中绑定gridview。该解决方案对我很有效。感谢支持该解决方案。控件的索引为回发后与ViewState中的数据源匹配。与预期相反,CommandArgument与GridView中的行没有直接关联。