We help IT Professionals succeed at work.
Get Started

How to pre-select 'some' checkboxes with CheckBoxList control?

gormdotus asked
Last Modified: 2008-01-09
I have a CheckBoxList control which populates categoryNames as names and categoryIDs as values.

I use this control to filter a list which I read from the same database. Right now when the page is opened on a browser you see no result until one of the check boxes is selected.

It populates three options (for now). I want to be able control which category names are pre-selected when a user opens that page in his/her browser.

For example;
would pre-select CatID 1 and 2

would pre-select CatID 2 and 3 only

Here's form I use, just in case I confused you already:
<script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        String CategorySQLpart = "";
        String GetCategorySelection = "WHERE (dbo.EventCategories.CategoryID = 0)";
        for (int i = 0; i < EventCategory.Items.Count; i++)
            if (EventCategory.Items[i].Selected)
                GetCategorySelection += " OR (dbo.EventCategories.CategoryID = " + EventCategory.Items[i].Value + ") ";
        GetCategorySelection = GetCategorySelection.TrimEnd('R').TrimEnd('O');

        CategorySQLpart = GetCategorySelection;

         * Which location is selected
        String LocationSQLpart = "";
        String GetLocationSelection = "";
        if (EventLocation.SelectedIndex > 0)
            GetLocationSelection = " WHERE ([LocationID] = " + EventLocation.SelectedIndex + ")";
            GetLocationSelection = " WHERE ([LocationID] > 0)";

        LocationSQLpart = GetLocationSelection;

        String SQLQuery = "SELECT EventDate, EventID, CategoryName, LocationName, EventTitle, EventDetail, LocationID, CategoryID FROM (SELECT dbo.Events.EventID, dbo.EventCategories.CategoryName, dbo.EventLocation.LocationName, dbo.Events.EventTitle, dbo.Events.EventDate, dbo.Events.EventDetail, dbo.Events.ListThisFrom, dbo.Events.ListThisTo, dbo.Events.LocationID, dbo.Events.CategoryID FROM dbo.EventCategories RIGHT OUTER JOIN dbo.Events ON dbo.EventCategories.CategoryID = dbo.Events.CategoryID LEFT OUTER JOIN dbo.EventLocation ON dbo.Events.LocationID = dbo.EventLocation.LocationID ReplaceMeCategorySQLpart ) AS derivedtable1 ReplaceMeLocationSQLpart AND (ISNULL(ListThisFrom, GETDATE()) <= GETDATE()) AND (ISNULL(ListThisTo, GETDATE()) >= GETDATE())";

        String NewQuery = "";
        NewQuery = SQLQuery.Replace("ReplaceMeCategorySQLpart", CategorySQLpart).Replace("ReplaceMeLocationSQLpart", LocationSQLpart);

        connEvents.SelectCommand = NewQuery;

        //ViewQuery.Text = NewQuery;

    <form id="form1" runat="server">
        <asp:SqlDataSource ID="connLocations" runat="server" connectionString="<%$ appSettings:myConnStr %>"
            SelectCommand="SELECT [LocationID], [LocationName] FROM [EventLocation]"></asp:SqlDataSource>
        <asp:DropDownList ID="EventLocation" runat="server" AutoPostBack="True" DataSourceID="connLocations"
            DataTextField="LocationName" DataValueField="LocationID" AppendDataBoundItems="True">
            <asp:ListItem Selected="True" Value="all">All locations</asp:ListItem>

        <asp:SqlDataSource ID="connCategories" runat="server" connectionString="<%$ appSettings:myConnStr %>"
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM [EventCategories]">
        <asp:CheckBoxList ID="EventCategory" runat="server" AutoPostBack="True" DataSourceID="connCategories"
            DataTextField="CategoryName" DataValueField="CategoryID" RepeatDirection="Vertical"

        <asp:SqlDataSource ID="connEvents"
        connectionString="<%$ appSettings:myConnStr %>"></asp:SqlDataSource>

        <asp:DataList ID="DataList1" runat="server" DataKeyField="EventID" DataSourceID="connEvents">
            <div id="Event-<%# Eval("EventID") %>" class="Event">
                    <li class="EventTitle"><%# Eval("EventTitle") %></li>
                    <li class="EventDate"><span>Date: </span><%# Eval("EventDate") %></li>
                    <li class="EventLocation"><span>Location: </span><%# Eval("LocationName") %></li>
                    <li class="EventCategory"><span>Category: </span><%# Eval("CategoryName") %></li>
                <div class="EventDetail">
                <%# Eval("EventDetail") %>


I appreciate any help for this newbee.
Watch Question
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE