今天做项目时,在使用DrowDownList时遇到一点小问题,记录一下,很简单的问题,希望大家不要拍砖呀。
今天在做项目时用到DrowDownList,需要装载用户的角色列表,用于选取,但是每个角色又会对应着不同的权限(共有三种,用0,1,2表示),在选中一个选项时时,既需要角色的名称,又需要角色的权限,所以我开始的想法就是用text记录角色名称(唯一的),用value记录角色权限,由于一共就三种权限,所以value肯定有重复的记录,当时认为这样做应该没问题,但是在选择时就出现问题了。另外,项目中在选择DrowDownList中的项时还会自动更新一些其他的信息,也就是Autopostback是打开的,如果不打开应该不会出现这样的问题。
DropDownList代码如下(数据是绑定的):
<
asp:DropDownList ID
=
"
ddlRole
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
onselectedindexchanged
=
"
ddlRole_SelectedIndexChanged
"
>
< asp:ListItem Value = " 0 " > a </ asp:ListItem >
< asp:ListItem Value = " 1 " > b </ asp:ListItem >
< asp:ListItem Value = " 2 " > c </ asp:ListItem >
< asp:ListItem Value = " 0 " > d </ asp:ListItem >
< asp:ListItem Value = " 1 " > e </ asp:ListItem >
< asp:ListItem Value = " 0 " > f </ asp:ListItem >
</ asp:DropDownList >
< asp:ListItem Value = " 0 " > a </ asp:ListItem >
< asp:ListItem Value = " 1 " > b </ asp:ListItem >
< asp:ListItem Value = " 2 " > c </ asp:ListItem >
< asp:ListItem Value = " 0 " > d </ asp:ListItem >
< asp:ListItem Value = " 1 " > e </ asp:ListItem >
< asp:ListItem Value = " 0 " > f </ asp:ListItem >
</ asp:DropDownList >
这样在选择a,d,f时,都会仅选中a,选不中d和f。当时以为是自动提交里面函数的一些问题,但是调试半天之后都没有,后面就想到是不是value里面值相同的问题。
所以就把代码改成了:
<
asp:DropDownList ID
=
"
ddlRole
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
onselectedindexchanged
=
"
ddlRole_SelectedIndexChanged
"
>
< asp:ListItem Value = " 0a " > a </ asp:ListItem >
< asp:ListItem Value = " 1b " > b </ asp:ListItem >
< asp:ListItem Value = " 2c " > c </ asp:ListItem >
< asp:ListItem Value = " 0d " > d </ asp:ListItem >
< asp:ListItem Value = " 1e " > e </ asp:ListItem >
< asp:ListItem Value = " 0f " > f </ asp:ListItem >
</ asp:DropDownList >
< asp:ListItem Value = " 0a " > a </ asp:ListItem >
< asp:ListItem Value = " 1b " > b </ asp:ListItem >
< asp:ListItem Value = " 2c " > c </ asp:ListItem >
< asp:ListItem Value = " 0d " > d </ asp:ListItem >
< asp:ListItem Value = " 1e " > e </ asp:ListItem >
< asp:ListItem Value = " 0f " > f </ asp:ListItem >
</ asp:DropDownList >
这样选择之后就不会出现上面的问题了。但读取value时需要做个只取前一位的操作。
这是项目中遇到的一个小问题,希望能帮到有相同问题的人。如果有不当之处,还请见谅。