c# DropDownList的一句代码不知道啥意思

来源:百度知道 编辑:UC知道 时间:2024/06/02 00:54:58
DropDownList1.Items[DropDownList1.SelectedIndex].Selected=false;
这句代码啥意思,运行之后也没效果啊
DropDownList1.Items.FindByText(e.Item.Cells[7].Text.Trim()).Selected=true;
还有这句话也不知道啥意思

DropDownList1.SelectedIndex表示DropDownList1当前选中项的索引
DropDownList1.Items[DropDownList1.SelectedIndex].Selected=false;表示让DropDownList1当前选中的项不被选中,但是这么写有问题,因为DropDownlist不像checkbox那样有明确的true,false,所以你看不到效果

DropDownList1.Items.FindByText(e.Item.Cells[7].Text.Trim()).Selected=true;表示设置DropDownList1的选中项为e.Item.Cells[7].Text

比如e.Item.Cells[7].Text的数字是三月,DropDownList1是一个选择月份的,哪么DropDownList1就会定位到三月
e.Item.Cells[7].Text.Trim()应该是从gridview之类的空间传进来的把trim()表示取出字符串中的空格

这个是当你一打开页面就选择这个DropDownList的第八个,如果你有这么多数据的话,没有就会报错!

DropDownList1.Items[DropDownList1.SelectedIndex].Selected=false;
这个很奇怪, 就是设置"选定的选项"为"不选定"状态.
DropDownList1.Items.FindByText(e.Item.Cells[7].Text.Trim()).Selected=true;
用文本(这个文本从e.Item.Cells[7].Text来,e这个参数应该从GridView或者DataList等数据控件来.)查询下拉列表的选项,并把该选项设置为选定状态.