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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox event handler not working 1

Status
Not open for further replies.

Gill1978

Programmer
Joined
Jun 12, 2001
Messages
277
Location
GB
Hi,

I got a list of checkbox's that when the user selects/ deselects them the OnSelectedIndexChanged is fired and the value of the checkbox(s) should be copied into the listbox.

However the eventhandler isn't being triggered.

This is what I have so far:

aspx page:
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);
    }
}

Any help would be much appreciated...

Thanks

J
 
Code:
<asp:CheckBoxList ID="CheckBoxList2" runat="server" [COLOR=blue]AutoPostBack="true"[/color]>
        <asp:ListItem Text="Presentation 2" Value="V:/projects/JG/PitchBook/PitchBook/Presentations/Test2.ppt"></asp:ListItem>
    </asp:CheckBoxList>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason ... but its still not firing the event when I add the Autopostback="true"

Can you see anything else amiss?

Thanks

J
 
Code:
<asp:CheckBoxList ID="CheckBoxList2" runat="server" AutoPostBack="true" [COLOR=blue]OnClicked="CheckBoxList1_CheckedChanged"[/color]>
        <asp:ListItem Text="Presentation 2" Value="V:/projects/JG/PitchBook/PitchBook/Presentations/Test2.ppt"></asp:ListItem>
    </asp:CheckBoxList>
the event name may not be correct. it's either onclicked, or onchecked. either way this needs to be declared.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Jason neither of those (onclicked, or onchecked) are available ... however I though this:
OnSelectedIndexChanged="CheckBoxList1_CheckedChanged"

covered the onclick event?

 
yes, that would be the event. your checkboxlist a list control so the server doesn't see checked/unchecked it only sees that the index was changed.

I haven't worked with checkboxlists. you will probally need to cycle through the control to determine which checkboxes are changed if you want a list of all the selected checkboxes.
I don't think it has a property/function which will return the selected/unselected items.

if you just want the value of that single checkbox use the selected index of the EventArgs.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I have the recursive code ... I just need the OnSelectedIndexChanged="CheckBoxList1_CheckedChanged" wvent to fire once clicked which it isn't doing ... I have no idea why not ... it just doesn't do anything when I select either checkbox ...
 
looking at your aspx file in the original post, you have 2 checkbox lists with 1 item each. do you mean for this, or do you want 1 checkbox list with 2 items.

this might be what your looking for
Code:
<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 Text="Presentation 2" Value="V:/projects/JG/PitchBook/PitchBook/Presentations/Test2.ppt" />
    </asp:CheckBoxList>
    <asp:ListBox ID="FileSelectedListBox" runat="server" />
</asp:Content>
Code:
public partial class Questionnaire : System.Web.UI.Page
{
    public void CheckBoxList1_CheckedChanged(object sender, EventArgs e)
    {
        FileSelectedListBox.Text = "<p>Selected Items(s):</p>";
        foreach(ListItem item in CheckBoxList1.Items)
        {
            if (item.selected)
            {
                FileSelectedListBox.Items.Add(New ListItem(item.Value, item.Text));
            }
            else
            {
                ListItem toRemove = null;
                foreach(ListItem listedItem in FileSelectedListBox.Items)
                {
                     if(listedItem.Value == item.Value)
                     {
                         toRemove = listedItem;
                         break;
                     }
                }
                if (toRemove != null)
                {
                    FileSelectedListBox.Items.Remove(toRemove);
                }
            }
        }
    }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hiya ...

Created the above as you've written it from scratch ... but it event isn't beeing fired ...
This is really weird ...

What am I missing ....


 
Nevermind me ... I was missing the AutoPostBack ... it's so close to being perfect ...

I'm getting an error when I deselect one of the checkbox's ... can you help??

Thanks

Julie
 
your checkboxlist is still missing the autopostback value.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi there,

I fixed the error ... but I notice that when I deselect a checkbox the value isn't actually removed from the listbox ...
 
incorrect
FileSelectedListBox.Items.Add(New ListItem(item.Value, item.Text));


correct
FileSelectedListBox.Items.Add(New ListItem(item.Text, item.Value));


this should fix the problem.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
OK this line wasn't bringing back the right match:

if (listedItem.Value == item.Value)

Changed it to this and it's now PERFECT!!
if (listedItem.Value == item.Text)

Thanks ever so much Jason ... much appreciated ..

J
 
One more tiny problem when I select the first checkbox I get the following in the list box:

Presentation 1

When I then also select the second checkbox I get the following in the list box:

Presentation 1
Presentation 1
Presentation 2

It seems to put it again without removing the original one ...

 
Code:
public partial class Questionnaire : System.Web.UI.Page
{
    public void CheckBoxList1_CheckedChanged(object sender, EventArgs e)
    {
        FileSelectedListBox.Text = "<p>Selected Items(s):</p>";
        FileSelectedListBox.Items.Clear();
        foreach(ListItem item in CheckBoxList1.Items)
        {
            if (item.selected)
            {
                FileSelectedListBox.Items.Add(New ListItem(item.Text, item.Value));
            }
            else
            {
                ListItem toRemove = null;
                foreach(ListItem listedItem in FileSelectedListBox.Items)
                {
                     if(listedItem.Value == item.Value)
                     {
                         toRemove = listedItem;
                         break;
                     }
                }
                if (toRemove != null)
                {
                    FileSelectedListBox.Items.Remove(toRemove);
                }
            }
        }
    }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks SOOOO much ... its perfect!!
 
Right this is the last thing ...
Is there anyway to change the code so that it keeps the order the user selects the checkbox, also when the user deselects
i.e if user selects presentation 2 then 1

the listbox should show:
Presentation 2
Presentation 1

Thanks

J
 
Code:
<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 Text="Presentation 2" Value="V:/projects/JG/PitchBook/PitchBook/Presentations/Test2.ppt" />
    </asp:CheckBoxList>
    <asp:ListBox ID="FileSelectedListBox" runat="server" text="Selected Items:" />
</asp:Content>
Code:
public partial class Questionnaire : System.Web.UI.Page
{
    public void CheckBoxList1_CheckedChanged(object sender, EventArgs e)
    {
        foreach(ListItem item in CheckBoxList1.Items)
        {
            if (item.selected && !FileSelectedListBox.Contains(item))
            {
                FileSelectedListBox.Items.Add(New ListItem(item.Text, item.Value));
            }
            else if (!item.selected && FileSelectedListBox.Contains(item))
            {
                FileSelectedListBox.Items.Remove(item);
            }
        }
    }
}
if Contains validates to false but the item does exist, or if you get "Item Doesn't Belong" error create a new ListItem(text, value); and use that for comparisons.

if that still doesn't work, use a foreach loop to loop through the items until the Values match.

NOTE: I moved the static text for the filelistbox to the aspx. no need to set it in code behind since it doesn't change. you also won't need the <p> tags.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hiya,

The FileSelectedListBox does not have Contains attribute ...
not sure what to change ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top