Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4251318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:38:44+00:00 2026-05-21T04:38:44+00:00

I know that this is not the first time a question about this issue

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:38:44+00:00Added an answer on May 21, 2026 at 4:38 am

    @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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this is a simple question for PHP guys but I don't
This is a question that I have been pondering for a long time ,
This just won't work. The problem is that I do not know enough to
I know that this is somewhat subjective, but I wonder if there is a
I know that this is subjective and all, but still, can you provide some
First of all, sorry for the too long question. I know that there are
I know that C# supports covariance in arrays like this : object[] array =
I know that in JavaScript, creating a for loop like this: for(int i =
I know that in C#, if you write ~MyClass() , this basically translates to
I followed this tutorial on configuring the Rails plugin ExceptionNotifier. I know that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.