j****n 发帖数: 107 | 1 一个dropdownlist,从数据库读取数据动态生成list item.
出现这样一个问题,无论选择了哪一个item,list.SelectedValue永远
等于排在第一的那个item值。
这是可能什么问题造成的?查了一遍又一遍,就是看不出错在哪里了。
哪位牛人来帮我看一下啊?同样的code在其它文件里都跑得好好的。
生成list的code如下:
DropDownList.Items.Clear();
foreach ( ListItem myItem in myListItems)
DropDownList.Items.Add(new ListItem(myItem.Text, myItem.Value));
DropDownList.SelectedIndex = -1;
多谢多谢! |
k****i 发帖数: 1072 | 2 In which event did you put this code?
If it's a webapp,put it in init but not page load
【在 j****n 的大作中提到】 : 一个dropdownlist,从数据库读取数据动态生成list item. : 出现这样一个问题,无论选择了哪一个item,list.SelectedValue永远 : 等于排在第一的那个item值。 : 这是可能什么问题造成的?查了一遍又一遍,就是看不出错在哪里了。 : 哪位牛人来帮我看一下啊?同样的code在其它文件里都跑得好好的。 : 生成list的code如下: : DropDownList.Items.Clear(); : foreach ( ListItem myItem in myListItems) : DropDownList.Items.Add(new ListItem(myItem.Text, myItem.Value)); : DropDownList.SelectedIndex = -1;
|
j****n 发帖数: 107 | 3 Thank u for ur reply.
But it is put in an Init_DropDownList event.
【在 k****i 的大作中提到】 : In which event did you put this code? : If it's a webapp,put it in init but not page load
|
b********s 发帖数: 12 | 4 你有没有设置dropdownlist的autopostback 为true?
【在 j****n 的大作中提到】 : Thank u for ur reply. : But it is put in an Init_DropDownList event.
|
k****i 发帖数: 1072 | 5 I would suggest you to move it to Page OnInit() and get rid of the line
'DropDownList.SelectedIndex = -1'(actually this line have no effect on
dropdownlist)
【在 j****n 的大作中提到】 : Thank u for ur reply. : But it is put in an Init_DropDownList event.
|
j****n 发帖数: 107 | 6 设置了,但是没有任何作用。
【在 b********s 的大作中提到】 : 你有没有设置dropdownlist的autopostback 为true?
|
j****n 发帖数: 107 | 7 Thanks, I'll try it tomorrow.
【在 k****i 的大作中提到】 : I would suggest you to move it to Page OnInit() and get rid of the line : 'DropDownList.SelectedIndex = -1'(actually this line have no effect on : dropdownlist)
|
G**T 发帖数: 388 | 8
I would suggest bind data to the dropdownlist in pageload
I did this in my application, never had this problem
seems somthhing is wrong at other places
I would
【在 j****n 的大作中提到】 : Thanks, I'll try it tomorrow.
|
k****i 发帖数: 1072 | 9 R u sure?
data binding in pageload will overwrite the value retrieve from viewstate and
would cause the problem that julyan encountered.Unless you use !IsPostBack
【在 G**T 的大作中提到】 : : I would suggest bind data to the dropdownlist in pageload : I did this in my application, never had this problem : seems somthhing is wrong at other places : I would
|
G**T 发帖数: 388 | 10
and
ur right, use NOT isPostBack
line
【在 k****i 的大作中提到】 : R u sure? : data binding in pageload will overwrite the value retrieve from viewstate and : would cause the problem that julyan encountered.Unless you use !IsPostBack
|
M******k 发帖数: 27573 | 11 en, i guess your solution is correct.
the re-initialize of dropdownlist in Page_Load
overwrite the viewstate, so he can't get selectedValue
in the event.
【在 k****i 的大作中提到】 : R u sure? : data binding in pageload will overwrite the value retrieve from viewstate and : would cause the problem that julyan encountered.Unless you use !IsPostBack
|