zoukankan      html  css  js  c++  java
  • 控件之美_常用业务组件写法

     编程过程中总会有些重复的东西,如果到处拷贝,既不容易维护,结构也很不清晰,这时候就可以把他们提取出来,根据其共性,架构属于自己的库,因为一直从事asp.net方面的开发,就结合实际情况,用vb.net举个例子。

    asp.net方面的开发,就结合实际情况,用vb.net举个例子。

           在做进销存等软件的时候,经常会用到仓库、部门、类别等基础资料,而且这些东西会反复用到,最常使用的是使用DropDownList控件,如果还没有选定,就列出所有的信息,如果打开已有记录,就显示已选定的信息,功能很简单,就举个仓库的例子。

    常用的代码如下,因为是个例子,异常处理就省略了:

    new store.GetData 返还的是个DataTable,里面有主键”StoreID”,名称”StoreName”字段

    Protected WithEvents DropStore As System.Web.UI.WebControls.DropDownList

    绑定仓库DropDownList列表

    如果StoreID=0,则选择内容为“”,否则选定该仓库

    Private Sub BindStore(Optional ByVal StoreID As Integer = 0)

            DropStore.DataSource = (New Store).GetData

            dropstore.DataValueField=StoreID

            dropstore.DataTextField =StoreName

            DropStore.DataBind()

            Dim listitem As ListItem = New ListItem("", "0")

            dropclass.Items.Add(listitem)

            If StoreID=0 Then

                  dropstore.SelectedIndex = dropstore.Items.Count 1

            Else

                  dropstore.Items.FindByValue(StoreID).Selected = True            

            End if

    End Sub

    这段代码放在需要选择、显示仓库的地方,确实能用,但是如果使用仓库的页面很多,这段代码就需要到处拷贝,既不方便,维护也很不方便,想象一下假如以后需要对仓位管理,DropDownList需要按层次显示库存的情况,那时就需要找到所有使用这段代码的部分,进行修改。

           重复的代码统一管理,这是现在编程的基本要求,从可重用性出发,如果把这个部分做成一个控件,在界面上拖拖拉拉,简单设几个属性就达到了目的,确实是个不错的想法,想到就要做到,下面我们就把它实现。

           新建一个项目,项目名称是JxcControls,类型为类库,添加一个文件,文件类型是自定义控件,名称StoreDropDownList,打开这个文件,我们可以发现编译器已经帮助我们做了很多东西了,不过这些东西的帮助不大,我们还需要做很多工作,把默认内容改为下面:

     

     

     

     

    Imports System.ComponentModel

    Imports System.Web.UI

    WebControl改为DropDownList

    <ToolboxData("<{0}:StoreDropDownList runat=server></{0}:StoreDropDownList>")> Public Class StoreDropDownList

        Inherits System.Web.UI.WebControls.DropDownList

     

     

     

     

        Dim _StoreId As String

     

     

     

     

    只读属性

        <Bindable(True), Category("Appearance"), DefaultValue("")> ReadOnly Property StoreName() As String

            Get

                Return Me.SelectedItem.Text

            End Get

        End Property

     

     

     

     

    设置当前仓库的StoreID,根据这个值控制DropDownList显示

        <Bindable(True), Category("Appearance"), DefaultValue("")> Property StoreId() As String

            Get

                Return _StoreId

            End Get

     

     

     

     

            Set(ByVal Value As String)

                _StoreId = Value

                Me.ClearSelection()

                Me.Items.FindByValue(Value).Selected = True

            End Set

        End Property

     

     

     

     

        Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)

            If Not Page.IsPostBack Then

                Dim dt As DataTable = GetStore()

                EnsureChildControls()     '确定服务器控件是否包含子控件。如果不包含,则创建子控件

                Dim listitem As WebControls.ListItem

                For Each drow As DataRow In dt.Rows

                    listitem = New WebControls.ListItem(drow("showName"), drow("ID"))

                    Me.Items.Add(listitem)

                Next

                Me.Items.Insert(0, New WebControls.ListItem("", "0"))

                MyBase.OnDataBinding(e)

            End If

        End Sub

        提供数据部分

        Private Function GetStore() As DataTable

            Return (New Store).GetData

        End Function

    End Class

     

     

     

     

    编译后,会生成文件JxcControls.dll,如果项目需要使用,就在工具箱添加StoreName控件,把它拖动到页面的相应位置,代码部分如下:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

         If Not IsPostBack Then

             ……        

             StoreDropDownList1.DataBind()

             BindFace()

          End If

    End Sub

     

     

     

     

    Private Sub BindFace

         ……

         如果已经选定仓库,为StoreID,则只需要如下设置即可

        StoreDropDownList1.StoreID=StoreID

         ……

    End Sub

     

     

     

     

    得到已经选定的仓库的名称

    Dim StoreName as string= StoreDropDownList1.StoreName

    获得已经选定的仓库的ID

    Dim StoreID as integer= StoreDropDownList1.StoreID

        采用同样的方法,就可以逐步构建属于自己的基础库,比如机构,部门,类别等,编程需要时,在工具栏添加相应的控件,拖拉几下,简单设置几个属性,方便快捷。

        为了得到更大的灵活性,采用这种方法需要注意数据的采集,在本例中就是(New Store).GetData部分,最常用到的是与数据的连接采用单态模式,各种不同数据的采集使用虚拟工厂方法,这样做,才能够使你的类库逐步丰富强大。

    从实践上看,采用控件的方法不仅仅能够减轻开发时的工作量,而且对后续的维护也是非常方便。

  • 相关阅读:
    ClickOnce發布經驗
    reporting Server組件不全引起的致命錯誤
    異步調用
    Usercontrol Hosted in IE
    MATLAB命令大全(转载)
    一种保护眼睛的好方法
    关于oracle自动编号
    An Algorithm Summary of Programming Collective Intelligence (1)
    An Algorithm Summary of Programming Collective Intelligence (3)
    An Algorithm Summary of Programming Collective Intelligence (4)
  • 原文地址:https://www.cnblogs.com/antony1029/p/290137.html
Copyright © 2011-2022 走看看