zoukankan      html  css  js  c++  java
  • 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;
                }

            }

  • 相关阅读:
    mysql查询字段取前3位,后3位,中间3位,去除前3位,去除后3位
    10月份四季度
    JavaScript箭头函数的立即执行函数实现三元表达式执行多条语句
    JavaScript判断是否是同一天
    项目经理:是兄弟就一起加班吧
    技术人员转型项目经理的角色转换
    项目经理入职后,如何快速管理项目
    如何解决项目成员之间的冲突?
    提高各方面沟通效率,是项目经理该去做的事
    项目计划太复杂?试试思维导图
  • 原文地址:https://www.cnblogs.com/lds85930/p/1801710.html
Copyright © 2011-2022 走看看