ListBox selections does not show up after autopostback
I know that this is not the first time a question about this issue has been posted here, but I haven't managed to find an answer to my question.
I have a number of ListBoxes on my page:
<tr>
<td class="loqhArea2Area">
<asp:ListBox ID="ListBox1Val1" class=开发者_开发百科"InputItem" runat="server" AutoPostBack="true"></asp:ListBox>
</td>
<td class="loqhArea3Area">
<asp:ListBox ID="ListBox2Val1" class="InputItem" runat="server" AutoPostBack="true"></asp:ListBox>
</td>
<td class="loqhArea4Area">
<asp:ListBox ID="ListBox3Val1" class="InputItem" runat="server"></asp:ListBox>
</td>
</tr>
These boxes are linked together in a sense, the choice in the first box is used to populate the second one and so fourth. In order to get the information from them I use this code snippet:
protected override void OnInit(EventArgs e)
{
// Do some other stuff ...
if (!IsPostBack)
{
// Fill the boxes on initial load
}
else
{
// INeedTheData takes an ID-string (in this case "Val1")
// and the selected indexes as ints
INeedTheData("Val1",
ListBox1Val1.SelectedIndex,
ListBox2Val1.SelectedIndex,
ListBox3Val1.SelectedIndex);
}
// Some error handling
}
The problem is that the SelectedIndexes all returns -1
, which obviously is not what I need.
I have been googleing like crazy for a solution to this problem. All clues or leads are welcome. Thanks in advance!
Update: Maybe this can be of any clue to anyone, my predecessor (who I have not been able to reach unfortunately) implemented this rather strange code that actually works. Or should I say sort of works. The thing is we wanted some kind of more reliable code so I set out to re-write it.
INeedTheData("Val1"
, Request.Form.AllKeys.Contains("ctl01$ListBox1Val1") ? Request.Form["ctl01$ListBox1Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox1Val1"]) : 0
, Request.Form.AllKeys.Contains("ctl01$ListBox2Val1") ? Request.Form["ctl01$ListBox2Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox2Val1"]) : 0
, Request.Form.AllKeys.Contains("ctl01$ListBox3Val1") ? Request.Form["ctl01$ListBox3Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox3Val1"]) : 0);
This solution is not desirable since it gets the data using hardcoded html id's, which may be subject to change when rebuilding and reorganizing stuff on the page in the future. Anyhow I thought it should be entered here since it is the reason for me to rewrite it.
As stated above, all comments are welcome! Thanks!
Update ii (answer to @Deeptechtons): Desired behaviour
I have a group of three ListBoxes used to navigate and make choices from a tree graph. The first box (ListBox1Val1
) is populated directly from a database. The second (ListBox2Val1
) is empty until the user has selected his choice in the first. Doing so causes the children of the selected node in the first listbox to load into the second. Same thing goes for listbox number three (ListBox3Val1
). Select a node in the second box and the third one is populated.
@dotmartin Here is the code you need on the Cs file
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.DataSource = GetList();
ListBox1.DataBind();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox2.DataSource = GetSecondList(ListBox1.SelectedIndex);
ListBox2.DataBind();
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox3.Items.Add(new ListItem(ListBox1.SelectedValue + "-" + ListBox2.SelectedValue, "Wippie"));
}
private ListItemCollection GetList()
{
ListItemCollection lstNumbers = new ListItemCollection();
lstNumbers.Add(new ListItem("1", "One"));
lstNumbers.Add(new ListItem("2", "Two"));
lstNumbers.Add(new ListItem("3", "Three"));
lstNumbers.Add(new ListItem("4", "Four"));
lstNumbers.Add(new ListItem("5", "Five"));
return lstNumbers;
}
private ListItemCollection GetSecondList(int iSelectedIndex)
{
ListItemCollection lstRandom = new ListItemCollection();
System.Random RandNum = new System.Random();
for (int i = 0; i < 10; i++)
{
lstRandom.Add(new ListItem(RandNum.Next(ListBox1.SelectedIndex, i + 1).ToString(), "random"));
}
return lstRandom;
}
i had just generated some random numbers to be binded to the listbox.
Below is the aspx file code,
<form id="form1" runat="server">
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel id="UpdatePanel1" runat="server" updatemode="Conditional" childrenastriggers="true">
<ContentTemplate>
<div>
<asp:ListBox id="ListBox1" autopostback="true" runat="server" onselectedindexchanged="ListBox1_SelectedIndexChanged"
width="200"></asp:ListBox></div>
<div>
<asp:ListBox id="ListBox2" autopostback="true" runat="server" onselectedindexchanged="ListBox2_SelectedIndexChanged"
width="200"></asp:ListBox></div>
<div>
<asp:ListBox id="ListBox3" autopostback="true" runat="server" width="200"></asp:ListBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Actually, in ASP.NET, control events happen after a page load in the page life-cycle: ASP.NET Page Life Cycle Overview.
I guess (but not sure about the exact name) the drop-down should have a SelectedIndexChanged
event, where you should think of taking the new selection.
I hope this helps!
精彩评论