遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

GridView + ObjectDatasource 的一个范例代码

aspx页面

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CraftDataAdmin.aspx.cs" Inherits="PDAJob.PDAService.MES.CraftDataAdmin" %>
<%@ Register assembly="F.Studio.WebControls" namespace="F.Studio.WebControls" tagprefix="fs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>下料数据管理</title>
    <link type="text/css" rel="stylesheet" href="/Styles/Site.css"/>
</head>
<body>
    <form id="form1" runat="server">
                   <table style="width:100%" class="tb">
               <tr>
               <td>
                   料号:<asp:TextBox ID="txtKey" runat="server" Width="127px"></asp:TextBox>
                   类型:<asp:DropDownList ID="ddlPartType" runat="server">
                   </asp:DropDownList>
                   <asp:CheckBox ID="ckbIsLcok" runat="server" Text="锁定" />
                   <asp:Button ID="btnQuery" runat="server" onclick="btnQuery_Click" Text="查询" />
                   </td>
                   <td>
                       <asp:Button ID="btnLock" runat="server" Text="锁定物料" onclick="btnLock_Click" />&nbsp;
                       <asp:Button ID="btnUnlock" runat="server" Text="解锁物料" 
                           onclick="btnUnlock_Click" />
                   </td>
               </tr>
               </table>
    <fs:GridViewEx ID="gridViewEx" runat="server" AllowPaging="True" 
        AutoGenerateCheckBoxColumn="True" AllowSorting="True" AutoGenerateColumns="False" 
                       CheckBoxFieldHeaderWidth="3%" DataSourceID="ods" IsHoldState="True" 
                       DataKeyNames="RecId"   EmptyDataText="  没有符合条件的记录!"
                       PageSize="20" SerialText="" Width="100%">
        <Columns>
            <asp:TemplateField>
            <HeaderStyle Width="20%" />
            <ItemTemplate>
            <a href="/MES/CraftBOM.aspx?MNo=<%#Eval("MNo") %>" target="_blank">BOM</a> <a href="/MES/CraftRef.aspx?MNo=<%#Eval("MNo") %>"  target="_blank">引用</a>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="IsLock" HeaderText="锁定" SortExpression="IsLock" />
            <asp:BoundField DataField="MNo" HeaderText="MNo" SortExpression="MNo" />
            <asp:BoundField DataField="MName" HeaderText="MName" SortExpression="MName" />
            <asp:BoundField DataField="CableName" HeaderText="CableName" 
                SortExpression="CableName" />
            <asp:BoundField DataField="OldPartNo" HeaderText="OldPartNo" 
                SortExpression="OldPartNo" />
            <asp:BoundField DataField="PartType" HeaderText="PartType" 
                SortExpression="PartType" />
            <asp:BoundField DataField="CarType" HeaderText="CarType" 
                SortExpression="CarType" />
            <asp:BoundField DataField="Setting" HeaderText="Setting" 
                SortExpression="Setting" />
            <asp:BoundField DataField="ModelType" HeaderText="ModelType" 
                SortExpression="ModelType" />
            <asp:BoundField DataField="Specification" HeaderText="Specification" 
                SortExpression="Specification" />
            <asp:BoundField DataField="Length" HeaderText="Length" 
                SortExpression="Length" />
            <asp:BoundField DataField="PeelLen1" HeaderText="PeelLen1" 
                SortExpression="PeelLen1" />
            <asp:BoundField DataField="PeelLen2" HeaderText="PeelLen2" 
                SortExpression="PeelLen2" />
            <asp:BoundField DataField="AT1" HeaderText="AT1" SortExpression="AT1" />
            <asp:BoundField DataField="AP1" HeaderText="AP1" SortExpression="AP1" />
            <asp:BoundField DataField="MT1" HeaderText="MT1" SortExpression="MT1" />
            <asp:BoundField DataField="MP1" HeaderText="MP1" SortExpression="MP1" />
            <asp:BoundField DataField="AT2" HeaderText="AT2" SortExpression="AT2" />
            <asp:BoundField DataField="AP2" HeaderText="AP2" SortExpression="AP2" />
            <asp:BoundField DataField="MT2" HeaderText="MT2" SortExpression="MT2" />
            <asp:BoundField DataField="MP2" HeaderText="MP2" SortExpression="MP2" />
            <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
            <asp:BoundField DataField="Diameter" HeaderText="Diameter" 
                SortExpression="Diameter" />
            <asp:BoundField DataField="KBNo" HeaderText="KBNo" SortExpression="KBNo" />
            <asp:BoundField DataField="WireNo" HeaderText="WireNo" 
                SortExpression="WireNo" />
            <asp:BoundField DataField="WireType" HeaderText="WireType" 
                SortExpression="WireType" />
            <asp:BoundField DataField="Unit" HeaderText="Unit" SortExpression="Unit" />
            <asp:BoundField DataField="cch1" HeaderText="cch1" SortExpression="cch1" />
            <asp:BoundField DataField="cch2" HeaderText="cch2" SortExpression="cch2" />
            <asp:BoundField DataField="XLMachine" HeaderText="XLMachine" 
                SortExpression="XLMachine" />
            <asp:BoundField DataField="YJMachine1" HeaderText="YJMachine1" 
                SortExpression="YJMachine1" />
            <asp:BoundField DataField="YJMachine2" HeaderText="YJMachine2" 
                SortExpression="YJMachine2" />
            <asp:BoundField DataField="MidShelf" HeaderText="MidShelf" 
                SortExpression="MidShelf" />
            <asp:BoundField DataField="FinalShelf" HeaderText="FinalShelf" 
                SortExpression="FinalShelf" />
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
                SortExpression="Quantity" />
            <asp:BoundField DataField="Remark1" HeaderText="Remark1" 
                SortExpression="Remark1" />
            <asp:BoundField DataField="Remark2" HeaderText="Remark2" 
                SortExpression="Remark2" />
                 <asp:BoundField DataField="LTime" HeaderText="锁定时间" SortExpression="L.LTime" />
            <asp:BoundField DataField="AddTime" HeaderText="AddTime" 
                SortExpression="AddTime" />
            <asp:BoundField DataField="EditTime" HeaderText="EditTime" 
                SortExpression="EditTime" />
            <asp:BoundField DataField="AddUser" HeaderText="AddUser" 
                SortExpression="AddUser" />
            <asp:BoundField DataField="EditUser" HeaderText="EditUser" 
                SortExpression="EditUser" />
            <asp:BoundField DataField="RecId" HeaderText="RecId" SortExpression="RecId" />
            
        </Columns>
            <HeaderStyle CssClass="tr_S01" />
            <EmptyDataRowStyle  HorizontalAlign="Center" Height="100px"  />
    </fs:GridViewEx>

                   <asp:ObjectDataSource ID="ods" runat="server" EnablePaging="True" SelectMethod="QueryDataTable" 
                    SelectCountMethod="QueryCount"
                       SortParameterName="sortExpress"  TypeName="PDAJob.PDAService.BLL.MDicMgr" 
                       onselecting="ods_Selecting">
                        
                       <SelectParameters>
                           <asp:Parameter Name="username" Type="String" />
                           <asp:Parameter Name="mType" Type="Int32" />
                           <asp:Parameter Name="key" Type="String" />
                           <asp:Parameter Name="isLock" Type="Boolean" />
                       </SelectParameters>
                   </asp:ObjectDataSource>

    </form>
</body>
</html>

aspx.cs代码

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PDAJob.PDAService.MES
{
    using NPOI;
    using NPOI.SS;
    using System.Data.OleDb;
    using EFModel;
    using NPOI.HSSF.UserModel;
    using System.IO;
    using System.Data;
    using Core;
    using BLL;
    using System.ComponentModel;
    public partial class CraftDataAdmin : AuthPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPartType.Items.Add("");
                ddlPartType.Items.Add(SysSetting.C_MType_FP);
                ddlPartType.Items.Add(SysSetting.C_MType_W);
            }
        }

        protected void ods_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["username"] = UserSetting.UserName;
            e.InputParameters["mType"] = ddlPartType.SelectedValue;
            e.InputParameters["key"] =txtKey.Text;
            e.InputParameters["isLock"] = ckbIsLcok.Checked;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                gridViewEx.DataBind();
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void btnLock_Click(object sender, EventArgs e)
        {
            try
            {
                var ids= gridViewEx.SelectList.ToString();
                var mgr = new MDicMgr();
                mgr.Lock(ids, UserSetting.UserName);
               gridViewEx.DataBind();
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        protected void btnUnlock_Click(object sender, EventArgs e)
        {
            try
            {
                var ids = gridViewEx.SelectList.ToString();
                var mgr = new MDicMgr();
                mgr.Unlock(ids, UserSetting.UserName);
                gridViewEx.DataBind();
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }
    }
}

 

CS部门

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Linq.Dynamic;

using F.Studio.Web.Utility;
using F.Studio.Common;
namespace PDAJob.PDAService.BLL
{
    using EFModel;
    using DAL;
    [System.ComponentModel.DataObject]
    public class MDicMgr
    {
        private int _Total = 0;

        public MDicMgr()
        {
            Ctx = new JL_MFG_DEVEntities();
            
        }
        private JL_MFG_DEVEntities Ctx { get; set; }
        public List<KB_Lable_MDic> Query(int startRowIndex, int maximumRows,string username,string mType,string key,string sortExpress)
        {

         
                var q = Ctx.KB_Lable_MDic.Where(ent => ent.EditUser == username);
                if (!string.IsNullOrEmpty(mType))
                {
                    q = q.Where(ent => ent.PartType == mType);
                }
                if (!string.IsNullOrEmpty(key))
                {
                    q = q.Where(ent => ent.MNo.Contains(key));
                }

                _Total = q.Count();//计算数量

                if (string.IsNullOrEmpty(sortExpress)) sortExpress = "RecId";
                q=q.OrderBy(sortExpress);
                return q.Skip(startRowIndex).Take(maximumRows).ToList();
           
        }
        public DataTable QueryDataTable(int startRowIndex, int maximumRows, string username, string mType, string key,bool isLock, string sortExpress)
        {
            #region SQL
            string cmdText = @"
            With LockList(MNo,IsLock,LTime,LUser)
            as
            (
              Select MNo,'是',max(AddTime),Max(AddUser) From Kb_Lable_M_Lock
              Group by MNo
            )
            SELECT Top 10000 d.[MNo],d.[MName],[CableName]
            ,[OldPartNo],[PartType],d.[CarType],[Setting]
            ,[ModelType],[Specification],[Length]
            ,[PeelLen1],[PeelLen2],[AT1],[AP1],[MT1],[MP1],[AT2]
            ,[AP2],[MT2],[MP2],[Color],[Diameter],[KBNo]
            ,[WireNo],[WireType],[Unit],[cch1],[cch2]
            ,[XLMachine],[YJMachine1],[YJMachine2],[MidShelf]
            ,[FinalShelf],[Quantity],[Remark1],[Remark2]
            ,d.[AddTime],[EditTime],[AddUser],[EditUser],d.[RecId]
            , L.IsLock,L.LUser,L.LTime
             FROM KB_Lable_MDic d Left Join  LockList L
            On d.MNo=L.MNo Where 1=1
            ";
            #endregion
            if (!string.IsNullOrEmpty(mType))
            {
                cmdText += string.Format(" And partType='{0}' ", mType);
            }
            if (!string.IsNullOrEmpty(key))
            {
                cmdText += string.Format(" And d.MNo like '%{0}%'",Util.Safe(key));
            }
            if (isLock)
            {
                cmdText += string.Format(" And L.isLock='是'");
            }
            if (string.IsNullOrEmpty(sortExpress)) sortExpress = "RecId";
            cmdText += string.Format(" Order by {0}", sortExpress);
            DataTable dt = SqlHelper.ExecuteDataset(DBCtx.ConnStr, CommandType.Text, cmdText).Tables[0];
            _Total = dt.Rows.Count;
            DataTable dt2 = dt.Clone();
            foreach(var dr in dt.AsEnumerable().Skip(startRowIndex).Take(maximumRows))
            {
                dt2.Rows.Add(dr.ItemArray);
            }
      
            return dt2;
           
        }
        public int QueryCount( string username, string mType, string key,bool isLock)
        {
            return _Total;
        }

        public void Lock(string ids,string username)
        {
            
            var recArr = StringHelper.ConvertToNumList(ids, ",").ToArray();
            var list=Ctx.KB_Lable_MDic.Where(ent => recArr.Contains((int)ent.RecId)).ToList();
            foreach (var m in list)
            {
                var mlock=new KB_Lable_M_Lock();
                mlock.AddTime=DateTime.Now;
                mlock.Cartype=m.CarType;
                mlock.MName=m.MName;
                mlock.AddUser=username;
                mlock.MNo = m.MNo;
                Ctx.KB_Lable_M_Lock.AddObject(mlock);
            }
            Ctx.SaveChanges();
        }
        public void Unlock(string ids, string username)
        {
            var recArr = StringHelper.ConvertToNumList(ids, ",").ToArray();
            var mNoArr = Ctx.KB_Lable_MDic.Where(ent => recArr.Contains((int)ent.RecId)).Select(ent =>  ent.MNo).ToArray();
            var mNos= mNoArr.Aggregate("",(total,it)=> total += string.Format("'{0}',",it)).TrimEnd(",".ToArray());
            string sql=string.Format("Delete From KB_Lable_M_Lock Where MNo In({0})", mNos);
            Ctx.ExecuteStoreCommand(sql);
        }
    }
}

启用分页后必须配置了SelectCountMethod,对应的参数才有值传进来

posted on 2013-03-15 13:43  遗忘海岸  阅读(372)  评论(0编辑  收藏  举报