关于.net中的dropdownlist控件绑定hashtable和时间控件选择问题

今天在开发过程中,遇到两个问题,最后都解决了,我把问题和解决方法贴出来,跟大家一起分享

第一个问题.时间控件的选择问题

先看效果

在空白的地方点一下,就会出来选择框,很方便

以下是参考的链接

http://www.cnblogs.com/esshs/archive/2005/03/29/127691.html

上面有源码,把dll文件编译之后,添加到引用就OK。

在调试的过程中经常出来一个bug,因为我使用了frame框架

Bug:控件放到frame 的页面里面使用得时候就会出错:“parent.event 为空或不是对象”
将InputCalendar.cs中output.WriteLine("parent.event.cancelBubble=true;");一句去掉即可。

第一个问题解决.

接下来第二个问题

hashtable绑定dropdownlist控件

参考了http://hi.baidu.com/changleiwd/blog/item/adccd28ff27fcfedf01f3625.html

先看代码

    Hashtable hash = new Hashtable(6);
   
        for (int i = 1; i < 6; i++)
        {
            SqlCommand cmd = new SqlCommand("select count(*) from yxlib where fac_no='" + i + "'", con);
            int count = Convert.ToInt32(cmd.ExecuteScalar());//从数据库中查询这条记录是否存在
            if (count > 0)
            {//如果存在则加入hashtable中


                hash.Add("" + i + "号变量站", i);
                
            }
            sort.Add("" + i + "号变量站", i);
        }
            
            DropDownList1.DataSource = hash;      //把hashtable作为数据源                 
            DropDownList1.DataTextField = "Key";// 绑定字段
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataBind();

最后我又发现了一个问题

就是用hashtable绑定的项不排序,很难看,所以,我采用sortlist

代码如下:

        SqlConnection con = dataOperate.createConData();
        con.Open();        
        SortedList sort = new SortedList(6);
        for (int i = 1; i < 6; i++)
        {
            SqlCommand cmd = new SqlCommand("select count(*) from yxlib where fac_no='" + i + "'", con);
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            if (count > 0)
            {


           sort.Add("" + i + "号变量站", i);
                
            }
            
        }
            
            
            DropDownList1.DataSource = sort;            
            DropDownList1.DataTextField = "Key";
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataBind();

效果图如下:

大家慢慢体会吧,不懂留言问我.

posted on 2010-05-25 16:15  陈天成  阅读(774)  评论(0)    收藏  举报