- PR -

DropDownList の動的追加で選択が取得できない??

1
投稿者投稿内容
SL
大ベテラン
会議室デビュー日: 2008/05/02
投稿数: 183
投稿日時: 2008-07-06 09:13
お世話になります。

今、SQLで投稿中ですがこの質問も教えてください。
DropDownList に下記のように Page_Load() で動的に項目を追加してこれを選択すると常に、0または、先頭の項目しか返してきません。
ListItemCollection listBoxData = new ListItemCollection();
// Add items to the collection.
listBoxData.Add(new ListItem("apples"));
listBoxData.Add(new ListItem("bananas"));
listBoxData.Add(new ListItem("cherries"));
listBoxData.Add("grapes");
listBoxData.Add("mangos");
listBoxData.Add("oranges");
// Set the ListItemCollection as the data source for ListBox1.
DropDownList3.DataSource = listBoxData;
DropDownList3.DataBind();

DropDownList3_SelectedIndexChanged() で、下記のように2パターンとも先頭項目を返す。(AutoPostback でここを読んでいる)
DropDownList list = (DropDownList)sender;
string stDevice2 = list.SelectedValue;
  :
string stDevice = (string)DropDownList3.SelectedValue;

コントロールのプロパティでデータバインドや項目の編集は、していません。
よろしくお願いします。
C# ASP.NET
べる
ぬし
会議室デビュー日: 2003/09/20
投稿数: 1093
投稿日時: 2008-07-06 11:42
ポストバックのときもDataBindしてません?Page_Loadにブレーク貼るとわかると思います。
それだとDropDownList3の状態が元に戻っちゃいます。IsPostBackでないときだけDataBindしてください。
SL
大ベテラン
会議室デビュー日: 2008/05/02
投稿数: 183
投稿日時: 2008-07-06 14:14
お世話になります。

> それだとDropDownList3の状態が元に戻っちゃいます
そういうことなんですね。
ありがとうございます。うまくいきました。
1

スキルアップ/キャリアアップ(JOB@IT)