给通用控件赋值

有看到一网友在论坛上问问题,Insus.NET尝试了一下,下面提供Insus.NET所理解的基础之上的解决方案,仅供参考,原问题如下:



在解决之前,先准备一些数据吧。端午节快到了,公司分福利,希望你们公司也有。数据集是一个Hashtable:

private Hashtable GetData()
    {
        Hashtable ht = new Hashtable();
        ht.Add(1, "粽子");
        ht.Add(2, "苹果");
        ht.Add(3, "雪梨");
        ht.Add(4, "荔技");
        ht.Add(5, "现金");
        return ht;
    }
View Code


在站点上创建一个网页,在.aspx页面上,写上下面代码:



到Insus.NET的博客上,下载两个给件,是专为ListControl绑定的,已经重构过了的:
http://www.cnblogs.com/insus/archive/2013/01/28/2880618.html

http://www.cnblogs.com/insus/archive/2009/12/13/1622884.html

解压之后,把DLL拉入BIN目录中。如:



然后,我们去.aspx.cs为页面的CheckBoxList,DropDownList,RadioButtonList控件绑定数据源。



接下来,我们想在文本框填入一些值,这个值能够指向那些控件中选项的值,如果没有对应的值,控件将是回归至默认(缺省值)的状态。由于每个控件是不同的对象,为了能让它们能够互动,有一致的动作,Insus.NET写一个接口:



下面,要看你需要什么控件来互动了,也就是什么控件来接收文本框的值了。需要的控件,均接中写一个适配器(Adapter),由于Insus.NET仅列举了三个,因此分别写三个适配器。

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

/// <summary>
/// Summary description for InsusCheckBoxList
/// </summary>
namespace Insus.NET
{
    public class InsusCheckBoxList: InsusAdapter
    {
        CheckBoxList _CheckBoxList;
        
        public InsusCheckBoxList()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void SetValue(WebControl wc, object value)
        {
            if (wc is CheckBoxList)
            {
                _CheckBoxList = (CheckBoxList)wc;

                foreach (ListItem li in this._CheckBoxList.Items)
                {
                    li.Selected = li.Text == value.ToString() ? true : false;                    
                }
            }
        }
    }
}
InsusCheckBoxList

 

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

/// <summary>
/// Summary description for InsusDropDownList
/// </summary>
namespace Insus.NET
{
   
    
    public class InsusDropDownList: InsusAdapter 
    {
       DropDownList _DropDownList;
        
        public InsusDropDownList()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void SetValue(WebControl wc, object value)
        {
            if (wc is DropDownList)
            {
                _DropDownList = (DropDownList)wc;

                this._DropDownList.ClearSelection();
                foreach (ListItem li in this._DropDownList.Items)
                {
                    if (li.Text == value.ToString ())
                        this._DropDownList.Items.FindByText(value.ToString()).Selected = true;                   
                }
            }
        }
    }
}
InsusDropDownList

 

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

/// <summary>
/// Summary description for InsusRadioButtonList
/// </summary>
namespace Insus.NET
{
    public class InsusRadioButtonList: InsusAdapter

    {
        RadioButtonList _RadioButtonList;
        
        public InsusRadioButtonList()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void SetValue(WebControl wc, object value)
        {
            if (wc is RadioButtonList)
            {
                _RadioButtonList = (RadioButtonList)wc;
                               
                foreach (ListItem li in this._RadioButtonList.Items)
                {
                    li.Selected = li.Text == value.ToString() ? true : false; 
                }
            }
        }
    }
}
InsusRadioButtonList

 

 如果还有基它,继续写出来......
下面就是Button按钮的Click事件:



好了,还是看看最终的效果吧:

 

以下内容,于2013-06-11 09:00添加:

早上来看到第一次所想的,有些地方,觉得设计不好。按照面向对象的设计理念来说,接口设计的藕合性过高,接口内方法SetValue需要带两个参数。理想的带一个参数为好。
因此,Insus.NET把接口改为:

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

/// <summary>
/// Summary description for InsusAdapter
/// </summary>
namespace Insus.NET
{
    public interface InsusAdapter
    {
        void SetValue(object value);
    }
}
View Code


这样一改的话,那怎样知道是给那个一个控制赋值? 解决这个问题,我们可以使用构造函数来处理。回到上面的那三个适配器InsusCheckBoxList,InsusDropDownList,InsusRadioButtonList,下面是重构好之后的适配器:

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

/// <summary>
/// Summary description for InsusCheckBoxList
/// </summary>
namespace Insus.NET
{
    public class InsusCheckBoxList : InsusAdapter
    {
        CheckBoxList _CheckBoxList;

        public InsusCheckBoxList(CheckBoxList checkBoxList)
        {
            this._CheckBoxList = checkBoxList;
        }

        public void SetValue(object value)
        {
            foreach (ListItem li in this._CheckBoxList.Items)
            {
                li.Selected = li.Text == value.ToString() ? true : false;
            }
        }
    }
}
InsusCheckBoxList

 

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

/// <summary>
/// Summary description for InsusDropDownList
/// </summary>
namespace Insus.NET
{
    public class InsusDropDownList : InsusAdapter
    {
        DropDownList _DropDownList;

        public InsusDropDownList(DropDownList dropDownList)
        {
            this._DropDownList = dropDownList;
        }
       
        public void SetValue(object value)
        {
            this._DropDownList.ClearSelection();
            foreach (ListItem li in this._DropDownList.Items)
            {
                if (li.Text == value.ToString())
                    this._DropDownList.Items.FindByText(value.ToString()).Selected = true;
            }
        }
    }
}
InsusDropDownList

 

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

/// <summary>
/// Summary description for InsusRadioButtonList
/// </summary>
namespace Insus.NET
{
    public class InsusRadioButtonList : InsusAdapter
    {
        RadioButtonList _RadioButtonList;

        public InsusRadioButtonList(RadioButtonList radioButtonList)
        {
            this._RadioButtonList = radioButtonList;
        }

        public void SetValue(object value)
        {
            foreach (ListItem li in this._RadioButtonList.Items)
            {
                li.Selected = li.Text == value.ToString() ? true : false;
            }
        }
    }
}
InsusRadioButtonList

 

重构之后,控件就可以在适配器new实例化时,就传入去了。



可以参考下面重构全过程:



以下内容,于2013-06-11 10:20添加:

此部分内容,只是重构一下comments的语句,因为每个控件的适配器有实作接口,所以使用多态来重构它们。



posted @ 2013-06-10 17:44  Insus.NET  阅读(762)  评论(0编辑  收藏  举报