查询控件、分页控件、页面展示控件,我的Web开发三大得力助手

在我的Database2Sharp中,Web界面生成的代码都集成了查询控件、分页控件、页面展示控件,这几个控件可以简化很多代码,而且统一了风格,熟悉使用后,对开发Web具有不可估量的作用。

下面介绍下几个控件的功能和使用方法:

查询控件
1. 根据设定的字段属性在界面呈现相应的说明及控件
2. 支持下拉列表之间的联动,支持输入数据的验证操作。
3. 支持查询历史记录记忆功能
4. 支持日期控件的集成
5. 支持移动省公司界面样式集成

查询控件属性说明: 
 SearchFields            查询字段属性集合
 SortFieldName         排序字段名称
 ConnectionString     数据库链接字符串
 ColumnWidth          每个字段表格部分显示的宽度,0表示不设置
 InSQL         指定查询表初始部分的脚本
 OutSQL      传出的参数化SQL语句
 PagerParameters        和参数化SQL语句对应的参数集合
 RowControls              每行放置多少控件
 ContentControlWidth  所有控件展示为相同长度

 SearchButtonImage 查询按钮的图片(可选)
 ResetButtonImage   重置按钮图片(可选)
 SearchButtonText    查询按钮文本(默认为“查询”)
 ResetButtonText     重置按钮文本(默认为“重置”)
 AppendedButtons    在查询按钮附近追加的按钮


FieldInfo属性说明(默认使用前三个属性即可):
 FieldName        数据库字段名称
 ViewName        字段的显示名称
 FieldType         字段类型

 DefaultValue     ListItem类型的默认值(用于下拉列表)
 Items               列表项目ListItem[]数组
 Enabled            控件是否可用(默认为True)
 TargetFieldName 联动时候的字段名称(用来定位联动的DropDownList)
 OnFillItem          用于添加联动列表内容的委托函数
 ColumnSpan      字段在布局中占多少个单元格

 

控件一般使用说明:
 1.页面代码

ContractedBlock.gif ExpandedBlockStart.gif Code
    <%@ Register TagPrefix="cc1" Namespace="WHC.SearchControl" Assembly="SearchControl" %>
    
<cc2:SearchControl ID="SearchControl1" runat="server" Width="100%" RowControls="3"
                        InSQL
="SELECT * FROM tb_acl_Roles" SearchButtonImage="http://www.cnblogs.com/Themes/Default/btn_search.gif"
                        ResetButtonImage
="http://www.cnblogs.com/Themes/Default/btn_reset.gif"></cc2:SearchControl>

 

 2.页面后台代码

ContractedBlock.gif ExpandedBlockStart.gif Code
    protected void Page_Load(object sender, EventArgs e)
    {
        
this.Pager1.PageRecords = this.PageSize;
        
if (!IsPostBack)
        {
        FieldInfo name 
= new FieldInfo("Name""角色名称", FieldType.String);
             FieldInfo remark 
= new FieldInfo("Remark""角色备注", FieldType.String);
  
            
this.SearchControl1.SearchFields = new FieldInfo[] { name, remark  };
        
this.SearchControl1.SortFieldNames = "ID";//"ID";
        this.SearchControl1.ConnectionString = Helper.GetConnectionString();
        
this.SearchControl1.ColumnWidth = new Unit[]{ Unit.Percentage(10), Unit.Percentage(23), Unit.Percentage(10), 
                                     Unit.Percentage(
23), Unit.Percentage(10), Unit.Percentage(23) }; //设置列的宽度
        Helper.SetSearchControlStyle(SearchControl1);//设置控件样式
        this.SearchControl1.ContentControlWidth = Unit.Pixel(180); //所有控件使用一个宽度
                
        BindData();
        }
        
    
this.SearchControl1.OutSQLValueChanged += new OutSQLChangedHandle(SearchControl1_OutSQLValueChanged);
    }

    
private void BindData()
    {
        
try
        {
            
this.Pager1.ConnectionString = this.SearchControl1.ConnectionString;
            
this.Pager1.PagerParameters = this.SearchControl1.PagerParameters;
            
this.Pager1.SQL = this.SearchControl1.OutSQL;            
        }
        
catch (Exception ex)
        {
            Helper.ShowError(
this, ex, false);
            
return;
        }
    }
    
    
protected void SearchControl1_OutSQLValueChanged(object sender, OutSQLChangedEventArgs e)
    {
        BindData();
    }

 

如果是需要下拉列表的联动操作,实现也很方便,下拉列表联动介绍(城市和地区的联动例子):

ContractedBlock.gif ExpandedBlockStart.gif Code
    private void Page_Load(object sender, EventArgs e)
    {  
            
if (!Page.IsPostBack)
            {
             FieldInfo cityInfo 
= new FieldInfo("City""城市", FieldType.String);
             cityInfo.Items 
= new ListItem[] { new ListItem("北京市""北京"), new ListItem("广州"), new ListItem("成都") };
             cityInfo.TargetFieldName 
= "Area";
             cityInfo.OnFillItem 
+= new AddItemHandler(this.OnFillItem);
         FieldInfo areaInfo 
= new FieldInfo("Area""地区", FieldType.String);
             areaInfo.Items 
= new ListItem[0];
            }
    }


    
private void OnFillItem(DropDownList ddListControl, string selItemValue)
    {
        ddListControl.Items.Clear();

        
if (selItemValue.Trim().Length > 0)
        {
            
string sql = string.Format("select Area from CityArea where City ='{0}' ", selItemValue);
                    
using (SqlConnection connection = new SqlConnection(this.SearchControl1.ConnectionString))
            {
                connection.Open();
                SqlCommand com 
= new SqlCommand(sql, connection);
                
using (IDataReader reader = com.ExecuteReader())
                {    
                    
while (reader.Read())
                    {
                             ddListControl.Items.Add(
new ListItem(reader["Area"].ToString()));
                    }
                }
            }
        }

        ddListControl.Items.Insert(
0new ListItem("--所有--"string.Empty));
    }

 

 

分页控件
和查询控件以及内置的Datagrid控件一起使用,可以实现功能:
1、可以进行分页(首页、上一页、下一页、末页),跳转到指定页面、数据导出功能等
2、支持表格头部排序功能,Datagrid支持表格样式迭代变化功能。
3. 支持移动省公司界面样式集成
4. 支持页面的页码和页面数量的数字记忆,

编辑控件属性说明:
 BindControlID 数据源控件,DataGrid的ID
 ExportEnabled 是否显示导出按钮
 ImageBasePath 图片地址的基础路径
 
 ConnectionString 数据库连接字符串
 PagerParameters  参数化集合
 SQL    分页SQL语句

控件使用说明:
 1.页面代码

ContractedBlock.gif ExpandedBlockStart.gif Code
<cc1:Pager ID="Pager1" runat="server" Height="20" BindControlID="dg" ImageBasePath="http://www.cnblogs.com/Themes/Default"
                    FirstImagePath
="btn_sy.gif" NextImagePath="btn_xyy.gif" PreviousImagePath="btn_syy.gif"
                    LastImagePath
="btn_wy.gif" GoImagePath="btn_go.gif" ExportImagePath="btn_export.gif"
                    ExportEnabled
="false" />

 

 2.页面后台代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
    private void Page_Load(object sender, EventArgs e)
    {
        
if(!this.IsPostBack)
        {
            BindData();
        }
    }
    
    
private void BindData()
    {
        
try
        {
            
this.Pager1.ConnectionString = this.SearchControl1.ConnectionString;
            
this.Pager1.PagerParameters = this.SearchControl1.PagerParameters;
            
this.Pager1.SQL = this.SearchControl1.OutSQL;
        }
        
catch (Exception ex)
        {
            Helper.ShowError(
this, ex, false);
            
return;
        }
    }

    
//DataGrid样式变化代码
    protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem
            
|| e.Item.ItemType == ListItemType.EditItem)
        {
        
//修改控件的值代码部分
        }

        
if (e.Item.ItemType == ListItemType.Item ||
       e.Item.ItemType 
== ListItemType.AlternatingItem ||
       e.Item.ItemType 
== ListItemType.EditItem)
        {
            e.Item.Attributes.Add(
"onmouseover""this.className='HighLightItem'");
        }

        
if (e.Item.ItemType == ListItemType.Item)
        {
            e.Item.Attributes.Add(
"onmouseout""this.className='Item'");
        }
        
else if (e.Item.ItemType == ListItemType.AlternatingItem)
        {
            e.Item.Attributes.Add(
"onmouseout""this.className ='AlternatingItem'");
        }
    }

    
//排序头部处理
    protected void dg_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        
if (e.Item.ItemType == ListItemType.Header)
        {
            
for (int i = 0; i < dg.Columns.Count; i++)
            {
                
if (dg.Columns[i].SortExpression == this.SearchControl1.SortFieldNames)
                {
                    TableCell c 
= e.Item.Cells[i];
                    
if (c.Controls.Count > 0 &&
                           c.Controls[
0].GetType().ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
                    {
                        Label lblArrow 
= new Label();
                        lblArrow.Font.Name 
= "Webdings";
                        
if (this.SearchControl1.SortFieldAscend)
                        {
                            lblArrow.Text 
= "5";
                        }
                        
else
                        {
                            lblArrow.Text 
= "6";
                        }
                        c.Controls.Add(lblArrow);

                        
break;
                    }
                }
            }
        }
    }
    
//排序部分代码
    protected void dg_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
        
if (e.SortExpression == this.SearchControl1.SortFieldNames)
        {
            
if (this.SearchControl1.SortFieldAscend)
            {
                
this.SearchControl1.SortFieldAscend = false;
            }
            
else
            {
                
this.SearchControl1.SortFieldAscend = true;
            }
        }
        
else
        {
            
this.SearchControl1.SortFieldNames = e.SortExpression;
            
this.SearchControl1.SortFieldAscend = true;
        }

        BindData();
      
    }

 

 

页面展示控件

1. 支持查看、增加、编辑三种类型的页面展现
2. 根据设定的字段属性在界面呈现相应的说明及控件
3. 支持布局和样式修改
4. 支持日期控件的集成
5. 支持移动省公司界面样式集成

编辑控件属性说明: 
 EidtFields  新增或者编辑页面的字段信息
 EntityObject  对应的实体类对象
 ControlType  标识是新增、编辑还是查看已有的数据,默认为新增
 RowControls  每行放置的控件组数目,默认为2个
 LabelHorizontalAlign 标签的水平对齐方式
 ContentControlWidth  所有控件展示为相同长度

 ColumnWidth  列的宽度集合,宽度使用Unit类型
 RowHeight  行的高度集合,高度使用Unit类型

 ShowMessageBox  界面输入转换错误的时候,是否显示对话框,默认为True
 ShowErrorAfter  界面输入转换错误的时候,错误信息是否放在控件的后面,默认为false 

 OnCancel  处理取消并返回的委托
 OnSaveData  处理保存数据的委托


FieldInfo属性说明(默认使用前三个属性即可):
 FieldName  数据库字段名称
 ViewName  字段的显示名称
 FieldType  字段类型

 Items   DropDownList或者CheckBoxList的列表项目。
 ItemsType  下拉列表的类别:DropDownList或者CheckBoxList
 DefaultValue  ListItem类型的默认值(用于下拉列表)
 TargetFieldName  联动时候的字段名称(用来定位联动的DropDownList)
 OnFillItem  用于添加联动列表内容的委托函数
 IsRequired   字段是否必填项,默认为False (如果是必填项,在界面上要求输入内容)
 Enabled   控件是否可用,默认为True
 ColumnSpan  字段在布局中占多少个单元格
 
 ToolTip   控件的提示文本(只能设置文本框的提示)
 MaxLength  文本框最大可以输入的内容长度
 TextBoxMode  文本款的呈现模式,可以是SingleLine、MulitLine、Passord类型
 TextRows  多行文本框显示的行数
 TextColumns  文本框显示的列字符数

控件使用说明:
 1.页面代码

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Register TagPrefix="cc1" Namespace="WHC.EditControl" Assembly="EditControl" %>
     
<cc1:EditControl id="EditControl1" RowControls="1" ShowErrorAfter="True" ShowMessageBox="True" runat="server" width="100%" 

SaveButtonImage
="http://www.cnblogs.com/Themes/Default/btn_savetobox.gif" CancelButtonImage="http://www.cnblogs.com/Themes/Default/btn_Cancel.gif"></cc1:EditControl>

 

 2.页面后台代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    private const string CONNECTION_STRING = "Server=localhost;Database=Test;uid=sa;pwd=123456";

    
private void Page_Load(object sender, EventArgs e)
    {
        
if(!this.IsPostBack)
        {
            BindData();
        }
        
        
this.EditControl1.OnCancel = new CancelHandler(this.OnCancel);
        
this.EditControl1.OnSaveData = new SaveDataHandler(this.OnSaveData);
    }
    
    
private void BindData()
    {
        ControlType controlType 
= GetControlType();
        
this.EditControl1.ControlType = controlType;
        
if(controlType == ControlType.Add)
        {
            EditControl1.EntityObject 
= new TestInfo();
        }
        
else
        {
            
int id = int.Parse(Request.QueryString["ID"]);
            EditControl1.EntityObject 
= FindByID(id);
        }
        
        FieldInfo nameInfo 
= new FieldInfo("Name""姓名", FieldType.String);
        nameInfo.IsRequired 
= true;
        nameInfo.ToolTip 
= "请输入用户名称";
        
if(controlType != ControlType.Add)
        {
            nameInfo.Enabled 
= false//设置“名称”不可编辑
        }

        FieldInfo cityInfo 
= new FieldInfo("City""城市", FieldType.String);
        cityInfo.Width 
= 100;
        cityInfo.DefaultValue 
= new ListItem[] {new ListItem("北京市""北京"), new ListItem("广州"), new ListItem("成都"), new ListItem("武汉"),};
        cityInfo.TargetFieldName 
= "Area";
        cityInfo.AddItem 
= new AddItemHandler(this.AddItem);

        FieldInfo areaInfo 
= new FieldInfo("Area""地区", FieldType.String);
        areaInfo.DefaultValue 
= new ListItem[0];

        FieldInfo manInfo 
= new FieldInfo("Man""是否男性", FieldType.Boolean);
        manInfo.DefaultValue 
= new ListItem[] {new ListItem("男性""True"), new ListItem("女性""False")};
        FieldInfo birthInfo 
= new FieldInfo("Birthday""出生日期", FieldType.DateTime);
        birthInfo.IsRequired 
= true;

        FieldInfo ageInfo 
= new FieldInfo("Age""年龄", FieldType.Numeric);

        
this.EditControl1.EidtFields = new FieldInfo[] {nameInfo, cityInfo, areaInfo, manInfo, birthInfo, ageInfo};

        
this.EditControl1.RowControls = 3//默认一行放置2个控件组
        this.EditControl1.ContentControlWidth = Unit.Pixel(180);//统一所有控件的长度
        this.EditControl1.ColumnWidth = new Unit[]{ Unit.Percentage(10), Unit.Percentage(20), Unit.Percentage(10), 
                                                    Unit.Percentage(
20), Unit.Percentage(10), Unit.Percentage(20) }; //设置列的宽度
    }
    
    
private ControlType GetControlType()
    {
        ControlType controlType;
        
string type = Request.QueryString["type"];
        
switch(type)
        {
            
case "view"://查看页面
                controlType = ControlType.View;
                
break;
            
case "edit"://修改页面
                controlType = ControlType.Edit;
                
break;    
            
default ://增加页面
                controlType = ControlType.Add;
                
break;
        }
        
return controlType;
    }

    
//您可以使用自己的业务类来获取相关数据
    private object FindByID(int id)
    {
        TestInfo testInfo 
= new TestInfo();

        
string sql = string.Format("select * from test where id ='{0}' ", id);
        
using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
        {
            connection.Open();
            SqlCommand com 
= new SqlCommand(sql, connection);

            
using (IDataReader reader = com.ExecuteReader())
            {
                
if (reader.Read())
                {
                    testInfo.ID 
= Convert.ToInt32(reader["ID"]);
                    testInfo.Name 
= reader["Name"].ToString();
                    testInfo.Age 
= Convert.ToInt32(reader["Age"]);
                    testInfo.Area 
= reader["Area"].ToString();
                    testInfo.Birthday 
= Convert.ToDateTime(reader["Birthday"]);
                    testInfo.City 
= reader["City"].ToString();
                    testInfo.Man 
= Convert.ToBoolean(reader["Man"]);
                }
            }
        }

        
return testInfo;
    }

    
private void AddItem(DropDownList ddListControl, string selItemValue)
    {
        ddListControl.Items.Clear();

        
if (selItemValue.Trim().Length > 0)
        {
            
string sql = string.Format("select Area from CityArea where City ='{0}' ", selItemValue);
            
using (SqlConnection connection = new SqlConnection(CONNECTION_STRING))
            {
                connection.Open();
                SqlCommand com 
= new SqlCommand(sql, connection);
                
using (IDataReader reader = com.ExecuteReader())
                {
                    
while (reader.Read())
                    {
                        ddListControl.Items.Add(
new ListItem(reader["Area"].ToString()));
                    }
                }
            }
        }
    }


    
private void OnCancel()
    {
        Response.Redirect(
"Default.aspx");
    }

    
/// <summary>
    
/// 通过返回用户提交数据的实体类信息,您可以实现自己的新增保存、修改保存操作
    
/// </summary>
    
/// <param name="entity">新增或者修改的实体类信息</param>
    private void OnSaveData(object entity)
    {
        
if (this.EditControl1.ControlType == ControlType.Add)
        {
            
//在此保存新增页面的数据
            Response.Write("新增的页面数据:<br>");
            Response.Write(ReflectionUtil.GetProperties(entity));
        }
        
else if(this.EditControl1.ControlType == ControlType.Edit)
        {
            Response.Write(
"编辑的页面数据:<br>");
            Response.Write(ReflectionUtil.GetProperties(entity));
        }
    }

 

 查询控件、分页控件、页面展示控件的测试项目下载:http://files.cnblogs.com/wuhuacong/MyControlProjects.rar

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值