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

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

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

这时可以用DropDownList的DataBound事件

代码
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
++;
        }
    }

相关文章:

  • 2022-12-23
  • 2021-09-21
  • 2021-11-30
  • 2021-09-18
  • 2022-12-23
  • 2021-06-20
猜你喜欢
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
  • 2021-11-11
相关资源
相似解决方案