locked
Multiple choice and listbox RRS feed

  • Question

  • User-1614302739 posted

    Hello,

    I have two listbox with multiple choice. What I need to do is show the selected value in this format: (Listbox1FirstChoice) (Listbox2FirstChoice), (Listbox1SecondChoice) (Listbox2SecondChoice), (Listbox1ThirdChoice) (Listbox2ThirdChoice). I know how to show them when it's only one listbox, but with two it's a little confusing.

    Can someone please help me with the code?

    Wednesday, July 8, 2020 9:51 PM

All replies

  • User-939850651 posted

    Hi justSomeGirl,

    Based on your description, I created a simple example to fulfill your needs, and I added line breaks to more clearly demonstrate the results.

    Please refer to below code:

    Page code:
    
    <form id="form1" runat="server">
    <div>
    <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true"
    OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
    <asp:ListItem Text="Listbox1FirstChoice"></asp:ListItem>
    <asp:ListItem Text="Listbox1SecodeChoice"></asp:ListItem>
    <asp:ListItem Text="Listbox1ThirdChoice"></asp:ListItem>
    <asp:ListItem Text="Listbox1FourthChoice"></asp:ListItem>
    </asp:ListBox>
    <asp:ListBox ID="ListBox2" runat="server" AutoPostBack="true"
    OnSelectedIndexChanged="ListBox2_SelectedIndexChanged">
    <asp:ListItem Text="Listbox2FirstChoice"></asp:ListItem>
    <asp:ListItem Text="Listbox2SecodeChoice"></asp:ListItem>
    <asp:ListItem Text="Listbox2ThirdChoice"></asp:ListItem>
    <asp:ListItem Text="Listbox2FourthChoice"></asp:ListItem>
    </asp:ListBox>
    <br />
    <asp:Label ID="label1" runat="server"></asp:Label>
    </div>
    </form>
    Code behind:
    
    public partial class SortSelectItem : System.Web.UI.Page
        {
            public static int ListBox1SelectIndex;
            public static int ListBox2SelectIndex;
            public static List<string> TwoListBoxSelectChoice;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack) {
    ListBox1SelectIndex = -1;
    ListBox2SelectIndex = -1;
    TwoListBoxSelectChoice = new List<string>();
    } } private string getAllSelectChoice() { StringBuilder AllChoice = new StringBuilder(string.Empty); //loop the list for (int i = 0; i < TwoListBoxSelectChoice.Count; i++) { AllChoice.Append(TwoListBoxSelectChoice[i]); if (i < TwoListBoxSelectChoice.Count - 1) { AllChoice.Append(",</br>"); } } return AllChoice.ToString(); } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { ListBox1SelectIndex++; string ListBox1SelectOption = ListBox1.SelectedItem.Text; if (TwoListBoxSelectChoice.Count > ListBox1SelectIndex) { TwoListBoxSelectChoice[ListBox1SelectIndex] = ListBox1SelectOption + " " + TwoListBoxSelectChoice[ListBox1SelectIndex]; } else { TwoListBoxSelectChoice.Add(ListBox1SelectOption + " "); } label1.Text = getAllSelectChoice(); } protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e) { ListBox2SelectIndex++; string ListBox2SelectOption = ListBox2.SelectedItem.Text; if (TwoListBoxSelectChoice.Count > ListBox2SelectIndex) { TwoListBoxSelectChoice[ListBox2SelectIndex] = TwoListBoxSelectChoice[ListBox2SelectIndex] + ListBox2SelectOption; } else { TwoListBoxSelectChoice.Add(ListBox2SelectOption); } label1.Text = getAllSelectChoice(); } }

    Result:

    Hope this can help.

    Best regards,

    Xudong Peng

    Thursday, July 9, 2020 3:02 AM
  • User-1614302739 posted

    Thank you, this might be it. I just need to know one more thing... I have the listboxes on a Name.aspx, then with Name.aspx.cs i am redirecting it to DifferentName.aspx and then need to show those selected choices via DifferentName.aspx.cs. 

    So just by doing this :

    public partial class SortSelectItem : System.Web.UI.Page
        {
            public static int ListBox1SelectIndex;
            public static int ListBox2SelectIndex;
            public static List<string> TwoListBoxSelectChoice;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack) {
    ListBox1SelectIndex = -1;
    ListBox2SelectIndex = -1;
    TwoListBoxSelectChoice = new List<string>();
    } } private string getAllSelectChoice() { StringBuilder AllChoice = new StringBuilder(string.Empty); //loop the list for (int i = 0; i < TwoListBoxSelectChoice.Count; i++) { AllChoice.Append(TwoListBoxSelectChoice[i]); if (i < TwoListBoxSelectChoice.Count - 1) { AllChoice.Append(",</br>"); } } return AllChoice.ToString(); } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { ListBox1SelectIndex++; string ListBox1SelectOption = ListBox1.SelectedItem.Text; if (TwoListBoxSelectChoice.Count > ListBox1SelectIndex) { TwoListBoxSelectChoice[ListBox1SelectIndex] = ListBox1SelectOption + " " + TwoListBoxSelectChoice[ListBox1SelectIndex]; } else { TwoListBoxSelectChoice.Add(ListBox1SelectOption + " "); } label1.Text = getAllSelectChoice(); } protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e) { ListBox2SelectIndex++; string ListBox2SelectOption = ListBox2.SelectedItem.Text; if (TwoListBoxSelectChoice.Count > ListBox2SelectIndex) { TwoListBoxSelectChoice[ListBox2SelectIndex] = TwoListBoxSelectChoice[ListBox2SelectIndex] + ListBox2SelectOption; } else { TwoListBoxSelectChoice.Add(ListBox2SelectOption); } label1.Text = getAllSelectChoice(); } }

    I get an error that ListBox1 and ListBox2 are not recognized because they are on a different page... How do I fix this? Thank you very much

    Thursday, July 9, 2020 3:12 PM
  • User-939850651 posted

    Hi justSomeGirl,

    What you need to know is how to transfer data from one page to another. There are many ways to achieve such functions, such as Cookie or Session.

    There are some other ways to achieve this effect, please refer to this article.

    I wrote a simple example, using the session to achieve your needs, please refer to the following code:

    Page1 code:
    
    <form id="form1" runat="server">
            <div>
                <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true"
                    OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
                    <asp:ListItem Text="Listbox1FirstChoice"></asp:ListItem>
                    <asp:ListItem Text="Listbox1SecodeChoice"></asp:ListItem>
                    <asp:ListItem Text="Listbox1ThirdChoice"></asp:ListItem>
                    <asp:ListItem Text="Listbox1FourthChoice"></asp:ListItem>
                </asp:ListBox>
                <br />
                <asp:Label ID="label1" runat="server"></asp:Label>
                <br />
                <asp:Button Text="next page" runat="server" ID="jump" OnClick="jump_Click" />
            </div>
        </form>
    Page1 Code behind:
    
    protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            private string getAllSelectChoice(List<string> SelectList)
            {
                StringBuilder AllChoice = new StringBuilder(string.Empty);
                //loop the list 
                for (int i = 0; i < SelectList.Count; i++)
                {
                    AllChoice.Append(SelectList[i]);
                    if (i < SelectList.Count - 1)
                    {
                        AllChoice.Append(",</br>");
                    }
                }
                return AllChoice.ToString();
            }
    
            protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                List<string> TwoListBoxSelectChoice;
                if (Session["SelectItemList"] == null)
                {
                    TwoListBoxSelectChoice = new List<string>();
                }
                else
                {
                    TwoListBoxSelectChoice = (List<string>)Session["SelectItemList"];
    
                }
                int ListBox1SelectIndex;
                if (Session["ListBox1SelectIndex"] == null)
                {
                    ListBox1SelectIndex = -1;
                }
                else
                {
                    ListBox1SelectIndex = (int)Session["ListBox1SelectIndex"];
                }
                ListBox1SelectIndex++;
                string ListBox1SelectOption = ListBox1.SelectedItem.Text;
    
                if (TwoListBoxSelectChoice.Count > ListBox1SelectIndex)
                {
                    TwoListBoxSelectChoice[ListBox1SelectIndex] = ListBox1SelectOption + " " + TwoListBoxSelectChoice[ListBox1SelectIndex];
                }
                else
                {
                    TwoListBoxSelectChoice.Add(ListBox1SelectOption + " ");
                }
    
                Session["ListBox1SelectIndex"] = ListBox1SelectIndex;
                Session.Add("SelectItemList", TwoListBoxSelectChoice);
                label1.Text = getAllSelectChoice(TwoListBoxSelectChoice);
            }
    
            protected void jump_Click(object sender, EventArgs e)
            {
                Response.Redirect("SortSelectItem2.aspx");
            }
    Page2 code:
    
    <form id="form1" runat="server">
            <div>
                <asp:ListBox ID="ListBox2" runat="server" AutoPostBack="true"
                    OnSelectedIndexChanged="ListBox2_SelectedIndexChanged">
                    <asp:ListItem Text="Listbox2FirstChoice"></asp:ListItem>
                    <asp:ListItem Text="Listbox2SecodeChoice"></asp:ListItem>
                    <asp:ListItem Text="Listbox2ThirdChoice"></asp:ListItem>
                    <asp:ListItem Text="Listbox2FourthChoice"></asp:ListItem>
                </asp:ListBox>
                <br />
                <asp:Label ID="label1" runat="server"></asp:Label>
            </div>
        </form>
    Page2 Code behind:
    
    protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            private string getAllSelectChoice(List<string> SelectList)
            {
                StringBuilder AllChoice = new StringBuilder(string.Empty);
                //loop the list 
                for (int i = 0; i < SelectList.Count; i++)
                {
                    AllChoice.Append(SelectList[i]);
                    if (i < SelectList.Count - 1)
                    {
                        AllChoice.Append(",</br>");
                    }
                }
                return AllChoice.ToString();
            }
    
            protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                List<string> TwoListBoxSelectChoice;
                if (Session["SelectItemList"] == null)
                {
                    TwoListBoxSelectChoice = new List<string>();
                }
                else {
                    TwoListBoxSelectChoice = (List<string>)Session["SelectItemList"];
    
                }
                int ListBox2SelectIndex;
                if (Session["ListBox2SelectIndex"] == null)
                {
                    ListBox2SelectIndex = -1;
                }
                else {
                    ListBox2SelectIndex = (int)Session["ListBox2SelectIndex"];
                }
                ListBox2SelectIndex++;
                string ListBox2SelectOption = ListBox2.SelectedItem.Text;
    
                if (TwoListBoxSelectChoice.Count > ListBox2SelectIndex)
                {
                    TwoListBoxSelectChoice[ListBox2SelectIndex] = TwoListBoxSelectChoice[ListBox2SelectIndex] + ListBox2SelectOption;
                }
                else
                {
                    TwoListBoxSelectChoice.Add(ListBox2SelectOption);
                }
    
                Session["ListBox2SelectIndex"] = ListBox2SelectIndex;
                Session["SelectItemList"] = TwoListBoxSelectChoice;
                label1.Text = getAllSelectChoice(TwoListBoxSelectChoice);
            }

    Result:

    For more details about the session, you can refer to the following link:

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.session?view=netframework-4.8

    Best regards,

    Xudong Peng

    Friday, July 10, 2020 2:31 AM
  • User-1614302739 posted

    Appreciate it, thank you!

    Best regards

    Friday, July 10, 2020 6:08 PM