Loading

有关asp.net中dropDownlist的取值问题

将值绑定到dropdownlist中后,
this.DropDownList1.DataSource = getData(sql1);
this.DropDownList1.DataTextField = "categoryname";
this.DropDownList1.DataValueField = "categoryid";
this.DropDownList1.DataBind();
若想取categoryid的值,则直接用this.DropDownList1.SelectedValue;就可以了,而不管DropDownList1中是否有项,
即一项值也没有count=0,也不会报错。

而如果DropDownList1里一项都没有,用this.DropDownList1.SelectedItem取值(取的是categoryname)则会报“未将对象引用设置到对象的实例。”。

还有一点要注意的是,DataTextField对应与SelectedItem,
DataValueField对应于SelectedValue
所以取值的时候尽量用SelectedValue ,因为不管DropDownList1是否有值都不会异常,没值的时候SelectedValue 为空
posted @ 2008-07-15 19:55  .net's  阅读(3031)  评论(0)    收藏  举报