WEB开发技术

初见,Devexpress

绥江一百http://www.sj100.net                                                  欢迎,进入绥江一百感谢点击[我的小网站,请大家多多指教相互共勉]

1.连接数据库

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Web;
 4 using System.Data.OleDb;
 5 
 6 namespace myBPB
 7 {
 8     public class myDB
 9     {
10       OleDbConnection conn = null;
11       public OleDbConnection getConn()
12         {
13             string sServer = "192.168.132.10";
14             string sDatabase = "myBPB";
15             string sUser = "sa";
16             string sPassword = "12345678";
17             //Provider=SQLOLEDB; 指的是连接数据库所用的相关数据库驱动
18             //Data Source; 指的是服务器地址或服务器名称
19             //Persist Security Info; 指的是是否保存安全信息,其实可以简单的理解为"在数据库连接成功后是否保存密码信息",True表示保存,False表示不保存            
20             //Initial Catalog; 指的是要连接的数据库名称
21             //User ID; 登录数据库的名称
22             //Password; 登录数据库的密码
23             String sSQL = String.Format("Provider=SQLOLEDB;Data Source={0};" +"Persist Security Info=True;User ID={2};" +"Initial Catalog={1};Password={3};",sServer, sDatabase, sUser, sPassword);//数据库连接语句
24             try
25             {
26                 conn = new OleDbConnection(sSQL);
27                 conn.Open();
28                 return conn;
29             }
30             catch (System.Exception ex)
31             {
32                 return null;
33             }
34         }
35     }
36 }

连接数据库

2.填充ASPxGridView

 1 public void BindGridView()
 2         {
 3             OleDbConnection conn = new myDB().getConn();
 4             string str = "select * from 区域";
 5             OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
 6             DataSet ds = new DataSet();
 7             da.Fill(ds);
 8             this.ASPxGridView1.DataSource = ds.Tables[0].DefaultView;
 9             this.ASPxGridView1.DataBind();
10             conn.Close();
11         }

填充ASPxGridView

3.布局

控件:ASPxSplitter(分离器)

作用:布局时多用于分割页面

属性:Orientation(方向)= Vertical(垂直)或 Herizontal(水平)

        ASPxSplitter1.Panes[0].PaneStyle.Paddings.Padding = 0;         //第一个面板的内边距为0

        ASPxSplitter1.Panes[1].PaneStyle.Paddings.Padding = 0;         //第二个面板的内边距为0

        ASPxSplitter1.Panes[0].Size = 63;                                        //第一个面板的高度(大小)为63

        ASPxSplitter1.Panes[1].ScrollBars = ScrollBars.Auto;              //自动出现滚动条 

        ASPxSplitter1.AllowResize = false;                                       //不允许调整ASPxSplitter1

        ASPxSplitter1.FullscreenMode = true;                                   //全屏模式为true



控件:ASPxMenu1(菜单)

作用:制作菜单

属性:width(宽度)=100%

        Items(菜单的选项)

        ItemAutoWidth(菜单选项宽度自动调整)=True(真)或False(假)

        AutoSeparators=”RootOnly”(菜单选项之间用竖线隔开)

        ClientInstanceName=”ASPxMenu1″(控件的前端名称)



控件:表格

作用:排版

属性:Style—>表格—>table-layout=fixed(表格宽度为固定值)



控件:ASPxImage

作用:显示图标、图片

属性:ASPxImage1.ImageUrl = “~/Images/man.gif”;



控件:ASPxGridView

作用:显示数据

属性:ASPxGridView1.Width = 2000;

        ASPxGridView1.SettingsBehavior.AllowFocusedRow = true;//每行都可以选择

        Columns(列)— Caption(显示名称)— FieldName(数据库连接字段)

        AutoGenerateColumns=”False”//数据源自动绑定为假

        KeyFieldName=”bh”//关键字为bh

        ClientInstanceName=”ASPxGridView1″(控件的前端名称)

前端运用:

1.在每行前面添加一个ASPxCheckBox

 1         <dx:ASPxGridView ID="ASPxGridView1" runat="server">
 2            <Columns>
 3 
 4              <dx:GridViewCommandColumn Caption="操作" ShowSelectCheckbox="true" VisibleIndex="0" Width="20px">
 5              <HeaderTemplate>
 6              <dx:ASPxCheckBox ID="ASPxCheckBox1" runat="server" ToolTip="Select/Unselect all rows on the page"
 7              ClientSideEvents-CheckedChanged="function(s, e) { ASPxGridView1.SelectAllRowsOnPage(s.GetChecked()); }" />
 8              </HeaderTemplate>
 9              </dx:GridViewCommandColumn>
10 
11            </Columns>
12         </dx:ASPxGridView>

自动添加ASPxCheck.xml

2.在第一列显示行号

 1         <dx:ASPxGridView ID="ASPxGridView1" runat="server">
 2            <Columns>
 3 
 4               <dx:GridViewDataTextColumn Caption="序号" FieldName="XH" UnboundType="Integer" VisibleIndex="0"Width="36px">
 5               <Settings AutoFilterCondition="Contains" />
 6               <CellStyle HorizontalAlign="Center">
 7               </CellStyle>
 8               </dx:GridViewDataTextColumn>
 9 
10            </Columns>
11         </dx:ASPxGridView>

自动添加行号.xml

1         protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
2         {
3             if (e.Column.FieldName == "XH" && e.IsGetData)
4                 e.Value = (e.ListSourceRowIndex + 1).ToString();
5         }

自动添加行号.后台

3.将Columns分组

 1             <dx:GridViewBandColumn Caption="使用于">
 2                     <Columns>
 3                               <dx:GridViewDataTextColumn Caption="部门" FieldName="部门" 
 4                                    ShowInCustomizationForm="True" VisibleIndex="9" Width="150px">
 5                                    <Settings AutoFilterCondition="Contains" />
 6                                </dx:GridViewDataTextColumn>
 7                                <dx:GridViewDataTextColumn Caption="何工序或何设备" FieldName="何工序或何设备" 
 8                                    ShowInCustomizationForm="True" VisibleIndex="11" Width="200px">
 9                                    <Settings AutoFilterCondition="Contains" />
10                                </dx:GridViewDataTextColumn>
11                     </Columns>
12             </dx:GridViewBandColumn>

Columns.xml

4.ASPxComboBox配置数据源

1 <asp:SqlDataSource ID="SqlDataSource1_BuMen" runat="server" 
2             ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
3             ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
4             SelectCommand="SELECT DISTINCT [部门名称], [部门编码] FROM [部门设置]"></asp:SqlDataSource>

数据源—部门.xml

 1 <dx:ASPxComboBox ID="Com_BuMen" runat="server" 
 2                                     DataSourceID="SqlDataSource1_BuMen" ValueField="部门编码" DropDownStyle="DropDownList" 
 3                                     ValueType="System.String" TextFormatString="{1}" EnableCallbackMode="true" 
 4                                     IncrementalFilteringMode="Contains" TextField="部门编码" 
 5                                     ClientInstanceName="Com_BuMen" >
 6                                     <Columns>
 7                                     <dx:ListBoxColumn FieldName="部门编码" Width="45px" />
 8                                     <dx:ListBoxColumn FieldName="部门名称" Width="100%" />
 9                                     </Columns>
10                                 </dx:ASPxComboBox>

部门文本框.前台

5.增加

 1 public void Add()
 2 {
 3     String str_add= "insert into 区域(区域号,区域,备注) values('" + QuYuHao + "','" + QuYu + "','" + BeiZhu + "')";
 4     OleDbConnection conn = new myDB().getConn();
 5     OleDbCommand com = new OleDbCommand(str_add, conn);
 6     if(Convert.ToInt32(com.ExecuteNonQuery())>0)
 7     {
 8       Page.ClientScript.RegisterClientScriptBlock(typeof(QuYuEdit), "CustomEdit", "alert('添加成功!!');", true);
 9     }
10     else
11     {
12      Page.ClientScript.RegisterClientScriptBlock(typeof(QuYuEdit), "CustomEdit", "alert('添加失败!!');", true);
13     }
14      conn.Close();
15 }

增加.后台

6.删除

 1 public void Del()
 2         {
 3             String str_Del = "delete from 区域 where bh=" + 3;
 4             OleDbConnection conn = new myDB().getConn();
 5             OleDbCommand com = new OleDbCommand(str_Del, conn);
 6             int counts = com.ExecuteNonQuery();
 7             if (counts > 0)
 8             {
 9                 Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('删除成功!');", true);
10             }
11             else
12             {
13                 Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('删除失败!');", true);
14             }
15             conn.Close();
16         }

删除.后台

7.修改

 1 public void Update()
 2         {
 3             String str_Update = "update 区域 set 区域号='" + QuYuHao + "',区域='" + QuYu + "',备注='" + BeiZhu + "' where bh=" + Convert.ToInt32(ASPxHiddenField1.Get("SBH1"));
 4             OleDbConnection conn = new myDB().getConn();
 5             OleDbCommand com = new OleDbCommand(str_Del, conn);
 6             int counts = com.ExecuteNonQuery();
 7             if (counts > 0)
 8             {
 9                 Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('修改成功!');", true);
10             }
11             else
12             {
13                 Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('修改失败!');", true);
14             }
15             conn.Close();
16         }

修改.后台

8.查询

 1 public String Old_Name()
 2         {
 3             OleDbConnection conn = new myDB().getConn();
 4             string str = "select * from 物料编码 where bh='" + Convert.ToInt32(ASPxHiddenField1.Get("sBH")) + "'";
 5             OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
 6             DataSet ds = new DataSet();
 7             da.Fill(ds);
 8             String m = ds.Tables[0].Rows[0]["物料编码"].ToString();
 9             return m;
10         }

查询(DataAdapter、DataSet).后台

 1 public void Lei_Xing()
 2         {
 3             OleDbConnection conn = new myDB().getConn();
 4             string str = "select * from 基础数据 where 目录='供应商类型'";
 5             OleDbCommand com = new OleDbCommand(str, conn);
 6             OleDbDataReader read = com.ExecuteReader();
 7             while (read.Read())
 8             {
 9                 Txt_LeiXing.Items.Add(read["内容"].ToString());
10             }
11         }

查询后台(DataReader).后台

9.ASPxGridView中勾选ASPxCheckBox删除数据

 1 List<object> keyValues = this.ASPxGridView1.GetSelectedFieldValues("bh");  //需要在ASPxGridView1的源设置 KeyFieldName="bh"
 2 ASPxGridView1.Selection.UnselectAll();   //清空ASPxGridView中所有选择的语句。
 3 int count = keyValues.Count;
 4 if (str == "删除")
 5    {
 6      if (count <= 0)
 7        {
 8           Page.ClientScript.RegisterClientScriptBlock(typeof(WuLiaoBianMa), "CustomEdit", "alert('请选择要删除的数据!');", true);
 9           return;
10         }
11      foreach (object key in keyValues)
12         {
13           do_del(key.ToString()); //删除
14          }
15           keyValues.Clear();
16           count = keyValues.Count;
17           BindGridView();
18     }

ASPxGridView中勾选ASPxCheckBox删除数据

10.ASPxGridView过滤

后台:ASPxGridView1.Settings.ShowFilterRow = true;

前台:<Settings AutoFilterCondition="Contains" />//ASPxGridView属性—>Columns—>要实现过滤的列—>Settings—>AutoFilterCondition="Contains"

11.ASPxGridViewExporter导出

 1                 if (Directory.Exists(Server.MapPath("tmp")) == false)
 2                 {
 3                     Directory.CreateDirectory(Server.MapPath("tmp"));
 4                 }
 5                 string path = Server.MapPath("~/tmp/");//获取完整路径
 6                 ASPxGridViewExporter1.FileName = "物料编码";
 7                 ASPxGridViewExporter1.PageHeader.Font.Size = 14;
 8                 string message1 = @"物料编码";
 9                 ASPxGridViewExporter1.PageHeader.Center = message1;
10                 this.ASPxGridViewExporter1.WriteXlsToResponse();

ASPxGridViewExporter导出.后台

12.ASPxHiddenField隐藏字段

设置前台名称:ClientInstanceName="hf1";
后台赋初值:ASPxHiddenField1.Add("SBH",0);
前台赋值:hf1.Set('sBH', ASPxGridView1.GetRowKey(ASPxGridView1.GetFocusedRowIndex())); //给隐藏字段赋值为ASPxGridView1当前被选中行的bh值
后台取值:ASPxHiddenField1.Get("sBH");
前台取值:hf1.Get('sBH')

13.ASPxPopupControl自定义约会表单

设置前台名称:ClientInstanceName="ASPxPopupControl1";
前台显示ASPxPopupControl自定义表单:ASPxPopupControl1.Show();
前台隐藏ASPxPopupControl自定义表单:ASPxPopupControl1.Hid();

14.ASPxUploadControl上传、导入实例(导入)

点击导入按钮,弹出下图的自定义约会表单,将选择的文件上传至固定的地址并导入ASPxGridView中

导入对话框

 1 1.前台弹出表单:ASPxPopupControl1.Show();
 2 
 3 2.上传文件到指定的地址:
 4   String ExcelFileName = "";
 5   protected voidASPxUploadControl1_FileUploadComplete(objectsender,DevExpress.Web.ASPxUploadControl.
 6   FileUploadCompleteEventArgs e)
 7    {
 8             if (Directory.Exists(Server.MapPath("tmp")) == false)
 9             {
10                 Directory.CreateDirectory(Server.MapPath("tmp"));
11             }
12             if (e.IsValid)
13             {
14                 ASPxUploadControl1.PostedFile.SaveAs(Server.MapPath("~/tmp/")+ ASPxUploadControl1.PostedFile.FileName);
15                 ExcelFileName = ASPxUploadControl1.PostedFile.FileName;
16             } 
17    }
18 
19 3.导入ASPxGridView:
20   #region 从Table导出数据DaoRu()
21   public void DaoRu(ASPxGridView ASPxGridView1,String ExcelFileName)
22   {
23     try    //步骤一:先将用户选择的EXCEL文件上传至服务器端。
24     {
25            if (Directory.Exists(Server.MapPath("tmp")) == false)
26            {
27               Directory.CreateDirectory(Server.MapPath("tmp"));
28            }
29            string path = Server.MapPath("~/tmp/") + ExcelFileName;//获取完整路径              
30            GetOneSheet("Sheet", path, ASPxGridView1);
31 
32     }
33     catch
34     {
35        throw;
36      }
37   }
38 
39           //  步骤二:将上传的文件导入至DataSet
40   public static void GetOneSheet(string sheetname, string Path, ASPxGridView ASPxGridView1)
41   {
42             try
43             {
44               string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
45               OleDbConnection con = new OleDbConnection(connString); //连接excel表
46               con.Open();
47               String strOutPutExcel = "select * from [" + sheetname + "$]";
48               OleDbDataAdapter ad = new OleDbDataAdapter(strOutPutExcel, con);
49               DataSet table = table = new DataSet();
50               ad.Fill(table,"table1");  //填充dataset
51               int count = table.Tables[0].Rows.Count;
52               OleDbConnection conn = new myDB().getConn();
53               OleDbCommand com = null;
54               for (int i = 4; i < count; i++)
55               {
56 
57                     String QuYuHao = table.Tables[0].Rows[i][1].ToString().Trim();  
58                     String LeiBeiHao = table.Tables[0].Rows[i][2].ToString().Trim();
59                     String str_insert = "INSERT INTO [物料编码] ( [区域号], [类别号]) VALUES ( ,  )";
60 
61                     OleDbParameter op1 = new OleDbParameter();
62                     OleDbParameter op2 = new OleDbParameter();
63                   
64                     op1.OleDbType = OleDbType.VarChar;
65                     op2.OleDbType = OleDbType.VarChar;
66                     op1.Value = QuYuHao.ToString();
67                     op2.Value = LeiBeiHao.ToString();
68                     com = new OleDbCommand(str_insert,conn);
69                     com.Parameters.Add(op1);
70                     com.Parameters.Add(op2);
71                     try
72                     {
73                         com.ExecuteNonQuery();
74                     }
75                     catch 
76                     {
77                         //Debug.WriteLine();
78                     }
79                     
80                 }
81                 con.Close();
82             }
83             catch
84             {
85                 throw;
86             }
87   }
88         #endregion
89 
90 4.确定按钮单击事件中调用DaoRu():
91 DaoRu(ASPxGridView1, ExcelFileName);
92 BindGridView();  //重新填充ASPxGridView

代码实现

 15.根据ASPxGridView值得变化,前端给控件赋值实例(修改)

 1 1.ClientInstanceName="ASPxGridView1"   //要赋值的前端控件也需设置前端名称
 2 2.判断是否选择了"修改"
 3 if (str == "修改")
 4 {
 5     Session.Add("Add_Or_Update", 1);
 6      Response.Redirect("~/WuLiaoBianMaEdit.aspx"); //转到下图的修改页面
 7 }
 8 
 9 3."修改"页面加载:
10  if (!IsPostBack)
11      {
12            if (Convert.ToInt32(Session["Add_Or_Update"]) == 1)
13                 {
14                     ASPxImage1.ImageUrl = "~/Images/m5.gif";
15                     ASPxRoundPanel1.HeaderText = "修改";
16                     ASPxHiddenField1.Add("sBH2", 1);
17                     ASPxHiddenField1.Add("sBH", 0);
18                 }
19 }
20 
21 4.ASPxGridView单击事件:           
22   <ClientSideEvents FocusedRowChanged="function(s, e) { OnGridFocusedRowChanged(); }" />   
23   <ClientSideEvents FocusedRowChanged="function(s, e) { OnGridFocusedRowChanged(); }"></ClientSideEvents>   
24   <%--ASPxGridView的单击事件--%>
25 
26 5.Javascript实现ASPxGridView单击事件:
27   <script language="javascript" type="text/javascript">
28     function OnGridFocusedRowChanged()
29     {
30         ASPxGridView1.GetRowValues(ASPxGridView1.GetFocusedRowIndex(), '区域号;类别号', OnGetRowValues); //前端访问控件
31     }
32     function OnGetRowValues(values) 
33     {      
34         hf1.Set('sBH', ASPxGridView1.GetRowKey(ASPxGridView1.GetFocusedRowIndex())); //给隐藏字段赋值
35         if (hf1.Get('sBH2') == 1) 
36         {
37             Com_QuYuHao.SetText(values[0]);
38             Com_LeiBieHao.SetText(values[1]);
39         }
40     }
41   </script>

代码实现

16.选择”区域号”,”类别号”后,点击”已用流水号”按钮,从数据库中读取数据加载到表单中,并弹出表单(防止刷屏问题)

 

 1 4.ClientInstanceName="grid2"
 2 2.写一个自定义函数,填充表单中的ASPxGridView:
 3    public void LiuShuiHao(String QuYuHao,String LeiBieHao)
 4         {
 5             String str_LiuShuiHao = "";
 6             if (QuYuHao != "" && LeiBieHao != "")
 7             {  
 8                str_LiuShuiHao = "select * from 物料编码 where 区域号='" + QuYuHao + "' and 类别号='" + LeiBieHao + "'";
 9             }
10             else if (QuYuHao != "")
11             {
12                 str_LiuShuiHao = "select * from 物料编码 where  区域号='" + QuYuHao + "'";
13             }
14             else if (LeiBieHao != "")
15             { 
16                 str_LiuShuiHao = "select * from 物料编码 where  类别号='" + LeiBieHao + "'";
17             }
18             if(str_LiuShuiHao!="")
19             {
20             OleDbConnection conn = new myDB().getConn();
21             OleDbDataAdapter da = new OleDbDataAdapter(str_LiuShuiHao, conn);
22             DataSet ds = new DataSet();
23             da.Fill(ds);
24             this.ASPxGridView2.DataSource = ds.Tables[0].DefaultView;
25             this.ASPxGridView2.DataBind();
26             conn.Close();            
27             }
28         }
29 
30 3.在页面的load中调用该方法:
31     if (Com_QuYuHao.Text != "" || Com_LeiBieHao.Text != "")
32       {
33                 LiuShuiHao(Com_QuYuHao.Text, Com_LeiBieHao.Text);
34       }
35 
36 4.按钮的前端Click事件:
37    ASPxPopupControl1.Show();
38    grid2.Refresh();
39    e.processOnServer = false;
40 
41 读取数据库防止—刷屏

读数据库—防止刷屏

 

 

 

 

 

 

 

 

 

 

未经允许不得转载:WEB2.0开发设计 » 初见,Devexpress
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

大前端WP主题 更专业 更方便

联系我们联系我们