Asp .Net,排序网格视图错误

时间:2015-06-25 11:18:54

标签: c# asp.net

点击我的gridview中的电子邮件恶魔,没有任何反应。这是我的代码。请帮忙。新的asp .net。

我已设置ONSORT =" gvdetails_Sorting"和SORTING = TRUE

前端:

<asp:TemplateField HeaderText="Email Id">
    <ItemTemplate>
        <asp:Label ID="lblEmailId" runat="server" Text='<%# Bind("Email_Id") %>'
            Wrap="true" Width="100%" SortExpression="Email_Id">
        </asp:Label>                                         
    </ItemTemplate>

后端:

#region "Properties"
public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = SortDirection.Ascending;
        }
        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}
#endregion




protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.BindGrid();
    }

}

protected void BindGrid()
{
     try
    {
    Agent_Supervisor_Email obj = new Agent_Supervisor_Email();
    DataSet ds = new DataSet();
    ds = obj.FillGrid();
    gvdlist.DataSource = ds;
    gvdlist.DataBind();
    }
     catch (Exception ex)
     {
         ExceptionHandle(ex);
     }
}

private void SortGridView(string sortExpression, string direction)
{
    try
    {
        if ((ViewState["Data"] != null))
        {
            DataView dv = new DataView();
            dv = (DataView)ViewState["Data"];
            dv.Sort = sortExpression + " " + direction;
            gvdlist.DataSource = dv;
            gvdlist.DataBind();
        }
    }
    catch (Exception ex)
    {
        ExceptionHandle(ex);
    }
}

其他代码在此之间。然后是以后的代码。

 protected void gvdlist_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        string sortExpression = e.SortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, "DESC");
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, "ASC");
        }
    }
    catch (Exception ex)
    {
        ExceptionHandle(ex);
    }
}

它不会给我任何错误。但我的代码似乎没有。

1 个答案:

答案 0 :(得分:0)

1)检查您是否正确设置OnSorting和AllowSorting的GridView属性:

OnSorting="gvdlist_Sorting" AllowSorting="true"

2)如Markus所述,检查ViewState [&#34; Data&#34;]是否实际包含您的数据。由于您没有发布设置ViewState [&#34;数据&#34;]的代码,您可能没有将其设置在任何地方,因此您的排序代码实际上不会被运行。