• GridView数据绑定控件和ObjectDataSource数据源控件实现排序功能


    <asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" GridLines="Vertical"
             DataSourceID="odsUsers" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" Width="777px" DataKeyNames="Id" AllowPaging="True" PageSize="5" AllowSorting="True" OnSorting="gvMain_Sorting">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id"
                        Visible="False" />
                    <asp:BoundField DataField="LoginId" HeaderText="LoginId" ReadOnly="True" SortExpression="LoginId" />
                    <asp:BoundField DataField="LoginPwd" HeaderText="LoginPwd" SortExpression="LoginPwd"
                        Visible="False" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                    <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                    <asp:BoundField DataField="Mail" HeaderText="Mail" SortExpression="Mail" />
                    <asp:BoundField DataField="UserState" HeaderText="UserState" SortExpression="UserState"
                        Visible="False" />
                    <asp:BoundField DataField="UserRole" HeaderText="UserRole" SortExpression="UserRole"
                        Visible="False" />
                </Columns>
                <FooterStyle BackColor="#CCCC99" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            <asp:ObjectDataSource ID="odsUsers" runat="server" SelectMethod="GetAllUsersSort"
                TypeName="MyBookShop.BLL.UserManager" UpdateMethod="ModifyPartUser" DataObjectTypeName="MyBookShop.Models.User">          
                <SelectParameters>
                    <asp:Parameter DefaultValue="LoginId" Name="sort" Type="String" />
                </SelectParameters>
            </asp:ObjectDataSource>

    protected void gvMain_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["sort"] == null)
                ViewState["sort"] = "";
            if (ViewState["sort"].ToString() == e.SortExpression)
            {
                odsUsers.SelectParameters["sort"].DefaultValue = e.SortExpression + " desc";
                ViewState["sort"] = e.SortExpression + " desc";
            }
            else
            {
                odsUsers.SelectParameters["sort"].DefaultValue = e.SortExpression;
                ViewState["sort"] = e.SortExpression;
            }
            gvMain.DataBind();
            e.Cancel = true; //设置指示是否应取消事件的值。
        }

     public static IList<User> GetAllUsersSort(string sort)
    {
                string sql = "SELECT * FROM users order by " + sort;
                return GetUsersBySql(sql);
    }

    private static IList<User> GetUsersBySql( string safeSql )
            {
                List<User> list = new List<User>();

       try
       {
        DataTable table = DBHelper.GetDataSet( safeSql );
        
        foreach (DataRow row in table.Rows)
        {
         User user = new User();
         
         user.Id = (int)row["Id"];
         user.LoginId = (string)row["LoginId"];
         user.LoginPwd = (string)row["LoginPwd"];
         user.Name = (string)row["Name"];
         user.Address = (string)row["Address"];
         user.Phone = (string)row["Phone"];
         user.Mail = (string)row["Mail"];
         user.UserState = UserStateService.GetUserStateById((int)row["UserStateId"]); //FK
         user.UserRole = UserRoleService.GetUserRoleById((int)row["UserRoleId"]); //FK
     
         list.Add(user);
        }
     
        return list;
       }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }

            }

  • 相关阅读:
    python——(os, shutil)
    python-(subprocess, commands)
    PHP设计模式二:单例模式
    PHP设计模式一:工厂方法设计模式
    PHP垃圾回收机制
    PHP异常处理机制
    超文本传送协议HTTP
    IP地址
    Linux系统网络基本配置
    Linux系统LVM基本使用
  • 原文地址:https://www.cnblogs.com/lds85930/p/1801710.html
Copyright © 2020-2023  润新知