DropDownList DataBound事件

本文介绍了一种在ASP.NET中使用DropDownList控件时的高级绑定技巧。当DataSource中没有value值时,通常value会默认为Text值。文章分享了一个解决方法,通过在DataBound事件中设置ListItem的Value属性,实现自定义value值。

当我们在对DropDownList绑定数据的时候

DropDownList1.DataTextField = "";DataTextField 绑定要显示的值
DropDownList1.DataValueField = "";DataValueField 绑定value值

可是当绑定的时候没有value值的话,value的值就会和Text值相等,可是我们value值又不想取文字形式的

这时可以用DropDownList的DataBound事件

ContractedBlock.gifExpandedBlockStart.gif代码
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            
string[] BookingStatus = { "NoUse""未提交""已提交""已取消""受理中""已退回""已订妥""已过期" };
            DropDownList1.DataSource 
= BookingStatus;
            DropDownList1.DataBound 
+= new EventHandler(DropDownList1_DataBound);
            DropDownList1.DataBind();
        }
    }

    
void DropDownList1_DataBound(object sender, EventArgs e)
    {
        
int i = 0;
        
//ListControl此类由其他类(如 CheckBoxList、DropDownList、ListBox 和 RadioButtonList 类)继承以提供通用的基本功能
        ListControl list = sender as ListControl;
        
foreach (ListItem l in list.Items)
        {
            l.Value 
= i.ToString();
            i
++;
        }
    }

转载于:https://www.cnblogs.com/linlin/archive/2010/11/30/abc.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值