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 5988083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:54:32+00:00 2026-05-22T22:54:32+00:00

Im trying to get values from a ListBox that has multiselection and dynamic ListItems

  • 0

Im trying to get values from a ListBox that has multiselection and dynamic ListItems in it. My problem is that I can’t get the values once I press my “assign” button.

The idea is that I have like 10-15 listitems and they should be assigned with values. And I want to be able to select like 5 of them and press my assign button and then continue to assign other values to these items left in the list.

For som reason my ListBox.Item.Count always returns like 1 row.

Question: Is there something you need to think about when using ListBox in .NET 4.0 that isn’t obvious?

Parts of the code

Front:

 <fieldset>
    <legend>
       <asp:Label ID="Label1" runat="server" 
            Text="<%$ Resources:lang, ExtensionsTitleAB %>"></asp:Label>
    </legend>
    <div class="extLeft"> 
       <table>
          <tr>
             <td>
                <asp:Label ID="Label5" runat="server" 
                     Text="<%$ Resources:lang, ExtensionsTmNumber %>"></asp:Label>
             </td>
             <td>
                <asp:TextBox runat="server" ID="txtTmNumber"></asp:TextBox>
             </td>
          </tr>
          <tr>
             <td>
                <asp:Label ID="Label6" runat="server"
                     Text="<%$ Resources:lang, ExtensionsTmName %>"></asp:Label>
             </td>
             <td>
                <asp:TextBox runat="server" ID="txtTmName"></asp:TextBox>
              </td>
           </tr>
           <tr>
              <td>
                 <asp:Label ID="Label7" runat="server"
                      Text="<%$ Resources:lang, ExtensionsTmRegistered %>">
                 </asp:Label>
              </td>
              <td>
                 <asp:TextBox runat="server" ID="txtTmRegistered"></asp:TextBox>
              </td>
           </tr>
           <tr>
              <td>
                 <asp:Label ID="Label8" runat="server" 
                      Text="<%$ Resources:lang, ExtensionsTmLocality %>">
                 </asp:Label>
              </td>
              <td>
                 <asp:TextBox runat="server" ID="txtTmLocality"></asp:TextBox>
              </td>
           </tr>
           <tr>
              <td>
                 <asp:Label ID="Label9" runat="server" 
                      Text="<%$ Resources:lang, ExtensionsMemberShipId %>">
                 </asp:Label>
              </td>
              <td>
                 <asp:TextBox runat="server" ID="txtMembershipIdFaseOne">
                 </asp:TextBox>
              </td>
           </tr>
        </table>
     </div>
     <div class="extMiddle">            
        <asp:Button runat="server" ID="btnAssignAB" OnClick="btnAssign_Click"
                    Text="<%$ Resources:lang, ButtonAssignExtensions %>" /> 
     </div>
     <div class="extRight">
        <p><asp:Label ID="Label4" runat="server" 
                Text="<%$ Resources:lang, ExtensionsListHelp %>">
           </asp:Label>
        </p>
        <asp:ListBox runat="server" ID="listABContainer" 
                     SelectionMode="Multiple" AutoPostBack="false">
        </asp:ListBox>
     </div>
  </fieldset>    

The codebehind:

public partial class extensions : System.Web.UI.Page
    {
        private model.OrderHandling orderHandling;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["dataobject"] != null)
            {
                orderHandling = (OrderHandling)Session["dataobject"];
            }
            else
            {
                String url = "http://" + Request.Url.Authority + "/Default.aspx";
                Response.Redirect(url);
            }

            if (!IsPostBack)
            {
                addListItems();
            }                    
        }

        private void addListItems()
        {
            foreach (OrderLine line in orderHandling.order.getOrderLines())
            {
                if (line.price.getPriceType().Equals(Price_Types.SUNRISE_ONE) 
                  || line.price.getPriceType().Equals(Price_Types.SUNRISE_TWO))
                {
                    listABContainer.Items.Add(
                      new ListItem(line.domain.domainName, line.domain.domainName));

                    System.Diagnostics.Debug.WriteLine("added a domain " + 
                       "to list listABContainer : " + line.domain.domainName);
                }
                else if (line.price.getPriceType().Equals(Price_Types.LANDRUSH)
                        || line.price.getPriceType().Equals(Price_Types.GENERAL))
                {
                    listCDContainer.Items.Add(
                       new ListItem(line.domain.domainName, line.domain.domainName));

                    System.Diagnostics.Debug.WriteLine("added a domain " +
                       "to list listCDContainer : " + line.domain.domainName);
                }
            }            
        }

        protected void btnAssign_Click(object sender, EventArgs e)
        {                
                assignTMExtensions(); 
        }

        private bool assignTMExtensions()
        {
            bool success = true;

            TradeMarkExtension tmExt = new TradeMarkExtension();

            String errorMsg = "";

            if (!String.IsNullOrEmpty(txtTmNumber.Text) 
                  && 
               tmExt.isValid(EXTENSION_KEYS.TM_NUMBER, txtTmNumber.Text, null))
            {
                tmExt.setExtension(EXTENSION_KEYS.TM_NUMBER, txtTmNumber.Text);
            }               

            if (success)
            {
                System.Diagnostics.Debug.WriteLine("Succes: time for domainExtension, listCount : " + listABContainer.Items.Count);
                foreach (ListItem list in listABContainer.Items)
                {
                    if (list.Selected)
                    {
                        System.Diagnostics.Debug.WriteLine("Found a selected item " + list.Value);
                        try
                        {
                            OrderLine ol = orderHandling.order.getOrderLine(list.Value);
                            ol.domain.addExtension(tmExt);

                            System.Diagnostics.Debug.WriteLine("Addedd domainExtension");
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine("FAILED domainExtension");
                            showError.Text = "An exception has occured. Please reload the page and try again.";
                            return false;
                        }
                    }
                }
                Session["dataobject"] = orderHandling;
                removeListItems(listABContainer);
            }
            else
            {
                showError.Text = errorMsg;
            }

            return success;
        }

        private void removeListItems(ListBox list)
        {
            int i = 0;
            while(i < list.Items.Count)
            {
                if (list.Items[i].Selected)
                {
                    list.Items.RemoveAt(i);
                }

                i++;
            }
        }
    }
  • 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-22T22:54:33+00:00Added an answer on May 22, 2026 at 10:54 pm

    Something like this?

    ASPX page

    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" AutoPostBack="false" />
    <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" OnClick="ButtonSubmit_Click" />
    

    Code Behind

       protected void Page_Load(object sender, EventArgs e)
       {
          if (!IsPostBack)
          {
             DataBindListBox();
          }
       }
    
       protected void ButtonSubmit_Click(object sender, EventArgs e)
       {
          List<ListItem> selectedItems1 = ListBox1.Items.Cast<ListItem>().Where(li => li.Selected).ToList();
    
          // or
    
          string[] selectedItems2 = (Request.Form[ListBox1.UniqueID] ?? string.Empty).Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
       }
    
       private void DataBindListBox()
       {
          var data = Enumerable.Range(1, 5).Select(i => new { Text = i.ToString(), Value = i.ToString() }).ToList();
    
          ListBox1.DataSource     = data;
          ListBox1.DataTextField  = "Text";
          ListBox1.DataValueField = "Value";
          ListBox1.DataBind();
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 arrays that I'm trying to get the unique values only from
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I'm trying to get all property names / values from an Outlook item. I
I'm trying to get a query working that takes the values (sometimes just the
I'm trying to get the values from multiple textboxes using JQuery. I'm a bit
I'm trying to get the values from timestamps created in a lookup table. Let's
I'm trying to get longitude and latitude values from the google maps api with
What I'm trying to do is get 3 values from a key into separate
I'm trying to get a list of the unique values from a field called
So I'm trying to get the values from a SQLite database into a cursor,

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.