avatar
一个dropdownlist的问题# DotNet - 窗口里的风景
j*n
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;
多谢多谢!
avatar
k*i
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;

avatar
j*n
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

avatar
b*s
4
你有没有设置dropdownlist的autopostback 为true?

【在 j****n 的大作中提到】
: Thank u for ur reply.
: But it is put in an Init_DropDownList event.

avatar
k*i
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.

avatar
j*n
6
设置了,但是没有任何作用。

【在 b********s 的大作中提到】
: 你有没有设置dropdownlist的autopostback 为true?
avatar
j*n
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)

avatar
G*T
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.
avatar
k*i
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

avatar
G*T
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

avatar
M*k
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。