asp.net 数据表的排序

前台代码:

复制代码
<asp:HiddenField runat="server" ID="descing" />
                <asp:HiddenField runat="server" ID="dt" />
                <asp:Button runat="server" ID="sortbtn" onclick="lvVehicle_PagePropertiesChanged" style="display:none;" />

<td class="Tcenter" width=85 onclick="sort(3)">录入日期</td>

jquery code:
function sort(fid) {
    if ($("#sortfield").val() == fid) {
        if ($("#descing").val() == "asc") {
            $("#descing").val("desc");
        }
        else {
            $("#descing").val("asc");
        }
    }
    else {
        $("#sortfield").val(fid);
        $("#descing").val("asc");
    }
    document.getElementById("sortbtn").click();
    return false;
}
复制代码

后台代码:

protected void lvVehicle_PagePropertiesChanged(object sender, EventArgs e)
        {
           //此处为分页,数据重新绑定            
        }
复制代码
数据源绑定:
lvVehicle.DataSource = bllcehicl.GetAllVehiclelist("","",Convert.ToInt32(checkInt(sortfield.Value, 0)), descing.Value);
//此处传入排序的标记

DAL层编写:
public IQueryable GetAllVehiclelist("", int SortField, string Descing)
{
   xxxx xx=new xxxx();
   var query=xx.xxx.where(.....);
    switch (SortField)
            {
                //默认 提交日期
                case 0:
                default:
                    query = query.OrderByDescending(qu => qu.subDate);
                    break;
                case 1:
                    //车牌号
                    if (("desc").Equals(Descing))
                    {
                        query = query.OrderBy(qu => qu.Base_tabletNumber);
                    }
                    else
                    {
                        query = query.OrderByDescending(qu => qu.Base_tabletNumber);
                    }
                    break;
                case 2:
                    //分类
                    if (("desc").Equals(Descing))
                    {
                        query = query.OrderBy(qu => qu.InfType);
                    }
                    else
                    {
                        query = query.OrderByDescending(qu => qu.InfType);
                    }
                    break;
                    //.......                
}
return query;
}
复制代码

标签