Hi!

I need some help on this. I have the checkbox name chkitems: here's the data:

<asp:CheckBoxList runat="server" ID="chkitems" Font-Names="Tahoma" Font-Size="Smaller"
RepeatColumns="3" RepeatDirection="Horizontal">
 <asp:ListItem Value="1">Item 1</asp:ListItem>
 <asp:ListItem Value="2">Item 2</asp:ListItem>
 <asp:ListItem Value="3">Item 3</asp:ListItem>
 <asp:ListItem Value="4">Item 4</asp:ListItem>
 <asp:ListItem Value="5">Item 5</asp:ListItem>
</asp:CheckBoxList>

Below on this script is the button name "cmdSelectedItems". What I want to do is once I click the button(cmdSelectedItems) It should be automatically checked the Items 1,3 and 5.

Please help me.

Thanks in advance.

Dani AI

Generated

Quick summary: ’s server-side fix answered ’s request to check Items 1, 3 and 5. The following notes add robustness and an alternative client-side approach to avoid common gotchas (lost selections, null refs, unnecessary postbacks).

Data-binding and postbacks
Data-bound CheckBoxLists are often the root cause when selections don’t persist. Populate or call DataBind only when the page first loads — i.e., inside an IsPostBack check — so that programmatic selection applied on a button click isn’t immediately overwritten.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // bind or populate chkitems here
    }
}

Safe server-side selection
When selecting by value, confirm the item exists to avoid a NullReferenceException. Using LINQ makes the intent clear and handles missing values gracefully.

using System.Linq;

var item = chkitems.Items.Cast<ListItem>().FirstOrDefault(i => i.Value == "3");
if (item != null) item.Selected = true;

Client-side alternative (no postback)
To avoid a round-trip, set checked on the rendered input elements inside the CheckBoxList container. Using the control’s ClientID keeps the selector accurate when master pages are used.

<script>
function selectItems() {
  var boxes = document.querySelectorAll('#<%= chkitems.ClientID %> input[type=checkbox]');
  [0,2,4].forEach(function(i){ if (boxes[i]) boxes[i].checked = true; });
}
</script>

Notes and troubleshooting
Indexes map to items 1/3/5 as 0/2/4, but indexes are brittle if the list changes — prefer matching by value. Inspect the rendered HTML in browser devtools to verify input names/values. If partial updates are needed, consider an UpdatePanel or OnClientClick with return false to prevent a postback.

Recommended Answers

All 4 Replies

I'd suggest you do this client side, but if you want to handle this server-side...

aspx
<form id="form1" runat="server">
        <div>
            <asp:CheckBoxList runat="server" ID="chkitems" 
                   Font-Names="Tahoma" Font-Size="Smaller"
                    RepeatColumns="3" RepeatDirection="Horizontal">
                <asp:ListItem Value="1">Item 1</asp:ListItem>
                <asp:ListItem Value="2">Item 2</asp:ListItem>
                <asp:ListItem Value="3">Item 3</asp:ListItem>
                <asp:ListItem Value="4">Item 4</asp:ListItem>
                <asp:ListItem Value="5">Item 5</asp:ListItem>
            </asp:CheckBoxList>
            <asp:Button Text="Select Items" runat="server" 
                    ID="cmdSelectedItems" OnClick="cmdSelectedItems_Click" />
        </div>
        </form>
What language are you developing in? C#?
protected void cmdSelectedItems_Click(object sender, EventArgs e)
{
   chkitems.Items.FindByValue("1").Selected = true;
   chkitems.Items.FindByValue("3").Selected = true;
   chkitems.Items.FindByValue("5").Selected = true;
 }

This is exactly what I've been looking for.

Thank you very much.

Glad you solved your issue.

Please kindly mark this thread as solved. Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.