Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add checkbox values to list box

Status
Not open for further replies.

Gill1978

Programmer
Jun 12, 2001
277
GB
Hi,

Does anyone know how you can add the value of a checkbox (selected by user) to a listbox in the order that the user selects.

The users will select from a list of files in directory, and the locations will be added to a collection.

Any help with this would be much appreciated.

Thanks

J
 
I would use a checkboxlist and set AutoPostBack = FALSE. Then on a button click, loop through the items in the list and check which ones are checked and add them to your collection. This way there is only one postback.
 
jbenson001 ... would have done it that way ... but the list box needs to be updated one by one when the user selects a file so that they can change the order of the files they select.

If it wasn't for that I would have gone your way ...
 
I guess I don't understand what you are doing.. I thought you wanted to select a bunch of chekboxes and add those checked values to a list box..
 
never mind .. I got it it now.. you can still do it my way and allow the user to change the order once it is in the listbox..
 
I've only just started with .NET and c# ... so I was thinking the easiest way ... still surfing for examples of how to add the values with Items.Add

So far I have :

Code:
    protected void CheckBoxList1_CheckedChanged(object sender, EventArgs e)
    {
        ListItem litem;
        litem.Value = this.Items(

        FileSelectedListBox.Items.Add()
    }
 
I think I have it (or something anyway) ...
However the OnSelectedIndexChanged="CheckBoxList1_CheckedChanged"
isn't firing and I can't see why ... here a snippet:

aspx file:

Code:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Questionnaire.aspx.cs" Inherits="Questionnaire" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="CandlesPlaceHolder" Runat="Server">
    <asp:CheckBoxList ID="CheckBoxList1" runat="server"  OnSelectedIndexChanged="CheckBoxList1_CheckedChanged" >
        <asp:ListItem Text="Presentation 1" Value="V:/projects/JG/PitchBook/PitchBook/Presentations/Test1.ppt"></asp:ListItem>
    </asp:CheckBoxList>
    <asp:CheckBoxList ID="CheckBoxList2" runat="server">
        <asp:ListItem Text="Presentation 2" Value="V:/projects/JG/PitchBook/PitchBook/Presentations/Test2.ppt"></asp:ListItem>
    </asp:CheckBoxList>
    <asp:ListBox ID="FileSelectedListBox" runat="server"></asp:ListBox>
    
</asp:Content>

.cs file
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Questionnaire : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void CheckBoxList1_CheckedChanged(object sender, EventArgs e)
    {
        //ListItem litem;
        //litem.Value = this.Items(
        object i;
        FileSelectedListBox.Text = "<p>Selected Items(s):</p>";
        for (int i = 0; i <= CheckBoxList1.Items.Count - 1; i++)

            if (CheckBoxList1.Items(i).Selected)
            {
                FileSelectedListBox.Text += CheckBoxList1.Items(i).Text + "<br />";
            }
        
        //FileSelectedListBox.Items.Add(CheckBoxList1.Text);
    }
}

Thanks for any help ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top