.Net中结合iBatisNet查询的DropDownList数据绑定

本文介绍如何使用iBatisNet查询结果填充ASP.NET DropDownList控件,通过具体实例展示了如何从数据库获取天气和文章分类列表,并绑定到下拉框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实标题中写iBatisNet查询有点特殊化的意味。其实DropDwonList控件可以接受很多种类的结果集作为他的数据源。比如Set、HashMap、IList。这里我是采用了iBatisNet中常用的IList结果集作为数据源的。
        private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面

            
if(!IsPostBack)
            
{
                
//构造当前时间
                this.txtTime.Text = ClassLib.getCurrentTime();

                
//构造天气列表
                this.ddlWeather.DataSource = ClassLib.getWeatherList();
                
this.ddlWeather.DataValueField = "Id";
                
this.ddlWeather.DataTextField = "Weather";
                
this.ddlWeather.DataBind();

                
//按用户构造文章分类
                this.ddlCategory.DataSource = ClassLib.getCategoryList(ClassLib.getUserId());
                
this.ddlCategory.DataValueField = "Id";
                
this.ddlCategory.DataTextField = "Title";
                
this.ddlCategory.DataBind();
            }

        }

  为了增加程序的封装性,我把对数据库的查询动作封装到了一个统一的全局类库中(ClassLib.cs)。这个文件中包含了几个查询动作。这里举例其一:
        //得到天气列表
        static public IList getWeatherList()
        
{
            IList listWeather 
= Mapper.Instance().QueryForList("SelectAll",null);
            
return listWeather;
        }


        
//得到分类列表
        static public IList getCategoryList(int _userId)
        
{
            IList listCategory 
= Mapper.Instance().QueryForList("SelectByUserId", _userId);
            
return listCategory;
        }

  绑定动作是很容易懂的。声明DropDownList的数据源DataSource,接着绑定结果集中的特定列到Value和Text就可以了。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值