Answered by:
Custom DropDownList's -- selected index problem

Question
-
User1150900820 posted
Hello:
I've created a custom drop down list which inherits core DropDownList. The task of it is to establish connection and bind data from database. It works fine. But at one situation it got problem of selected index.
I've taken five of my custom DDL in a page. suppose those are FastCombo1, FastCombo2, FastCombo3, FastCombo4, FastCombo5 and FastCombo6.
FastCombo1, FastCombo4 and FastCombo6 bind during pageLoad. FastCombo2 depends on FastCombo1, FastCombo3 depends on FastCombo2. FastCombo5 depends on FastCombo4. Everything works fine except: If I select a value from FastCombo3 (problem occurs from combo3 only) after then if postback occurs the selected index is set to 0 always.
I've written code in the code behind:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 FastCombo2.Items.Add(new ListItem("Select", "0"));
6 FastCombo3.Items.Add(new ListItem("Select", "0"));
7 FastCombo5.Items.Add(new ListItem("Select", "0"));
8
9 FastCombo1.DataBind();
10 FastCombo1.Items.Add(new ListItem("Select", "0"));
11 FastCombo1.SelectedIndex = FastCombo1.Items.Count - 1;
12
13 FastCombo4.DataBind();
14 FastCombo4.Items.Add(new ListItem("Select", "0"));
15 FastCombo4.SelectedIndex = FastCombo4.Items.Count - 1;
16
17 BindCustomer();
18 }
19 }
20
21 protected void FastCombo1_SelectedIndexChanged(object sender, EventArgs e)
22 {
23 FastCombo2.WhereValue = FastCombo1.SelectedItem.Value;
24 FastCombo2.DataBind();
25
26 FastCombo2.Items.Add(new ListItem("Select", "0"));
27 FastCombo2.SelectedIndex = FastCombo2.Items.Count - 1;
28 }
29
30 protected void FastCombo2_SelectedIndexChanged(object sender, EventArgs e)
31 {
32 FastCombo3.WhereValue = FastCombo2.SelectedItem.Value;
33 FastCombo3.DataBind();
34
35 FastCombo3.Items.Add(new ListItem("Select", "0"));
36 FastCombo3.SelectedIndex = FastCombo3.Items.Count - 1;
37 }
38
39 protected void FastCombo4_SelectedIndexChanged(object sender, EventArgs e)
40 {
41 FastCombo5.WhereValue = FastCombo4.SelectedItem.Value;
42 FastCombo5.DataBind();
43
44 FastCombo5.Items.Add(new ListItem("Select", "0"));
45 FastCombo5.SelectedIndex = FastCombo5.Items.Count - 1;
46 }
47
48 private void BindCustomer()
49 {
50 FastCombo6.DataTable="Customers";
51 FastCombo6.DataTextField = "ContactName";
52 FastCombo6.DataValueField = "CustomerID";
53 FastCombo6.DataBind();
54
55 FastCombo6.Items.Add(new ListItem("Select", "0"));
56 FastCombo6.SelectedIndex = FastCombo6.Items.Count - 1;
57 }
I've found that in my custom control when the method calls:
1 protected override void Render(HtmlTextWriter output) 2 { 3 base.Render(output); 4 }
When it renders FastCombo3 its selected index is 0 always. I don't understand why this is happening. But all other combos selected index is correct.
Note: FastCombo3 depends on FastCombo2 and FastCombo2 depends on FastCombo1. So, 2 level dependency is there. Is it the cuz of problem. If yes what's the solution???
Razin
Monday, May 5, 2008 1:03 AM
Answers
-
User1150900820 posted
Hi:
I've found the problem and solved it. Actually most of the data value i've bound from database into FastCombo3 are similar. I mean: suppose there are 3 values in the DDL.
Data Text: Cat, dog, Rat
Data Value: 2, 3, 2
I've selected the third 2 (Rat) from DDL but first item is also 2 (Cat). So it displays Cat always. So, the data values were not unique and that was the problem.
Razin
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 5, 2008 5:31 AM
All replies
-
User-16411453 posted
You should load the data inside the control code, and track the selected items inside the control code. The page behind defeats the purpose of a custom control.Monday, May 5, 2008 3:52 AM -
User1150900820 posted
Hi:
I've found the problem and solved it. Actually most of the data value i've bound from database into FastCombo3 are similar. I mean: suppose there are 3 values in the DDL.
Data Text: Cat, dog, Rat
Data Value: 2, 3, 2
I've selected the third 2 (Rat) from DDL but first item is also 2 (Cat). So it displays Cat always. So, the data values were not unique and that was the problem.
Razin
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 5, 2008 5:31 AM