Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7766815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:31:06+00:00 2026-06-01T15:31:06+00:00

I have a page where users can select different documents files in a datalist

  • 0

I have a page where users can select different documents files in a datalist control. The documents are categorized based on categories using the on pre-render event handler. Documents are selected based on Checkbox controls (not Checkboxlist). So far so good. What I want to happen next is to put a ‘Select All’ checkbox beside each Category’s name which should select only checkboxes under that category. Here is the datalist control:

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Vertical" OnPreRender="DataList1_PreRender" DataKeyField="docid" EnableViewState="false">
<ItemTemplate>
    <table cellpadding="0" cellspacing="0" id="tbl_data">
        <tr>
            <td>
                <asp:Label ID="lblHeader" runat="server" Font-Bold="True" Font-Underline="True"></asp:Label>
             <asp:Label runat="server" id="lbl_cb_all" visible="false">Select All <input runat="server" id="cb_selectall" type="checkbox" value='<%# Eval("catid") %>' /> </asp:Label>
            </td>
        </tr>

        <tr runat="server" id="tr_data">
            <td>
                <asp:Label ID="lbl_categoryname" runat="server" Text='<%# Eval("categoryname") %>' Visible="false" /> <!-- Hide this; only used in Code Behind -->
                 <input runat="server" id="cb_docid" type="checkbox" value='<%# Eval("docid") %>' />
                  <asp:Hyperlink ID="hpl_docfileencr" Text='<%# Eval("docfileencr") %>' NavigateUrl='<%# "~/PDFEncr/" + DataBinder.Eval(Container.DataItem, "docfileencr") %>' Target="_blank"  runat="server" />
                <br />
            </td>
        </tr>
    </table>
</ItemTemplate>

and here is the OnPreRender code:

protected void DataList1_PreRender(object sender, EventArgs e)
{
    string strTempLabelCategory = "";
    foreach (DataListItem item in DataList1.Items)
    {



        Label lbl_categoryname = item.FindControl("lbl_categoryname") as Label;


        if (strTempLabelCategory.ToUpper() != lbl_categoryname.Text.ToString().ToUpper())
        {
            strTempLabelCategory = lbl_categoryname.Text.ToString().ToUpper();
            Label lblHeader = item.FindControl("lblHeader") as Label;
            lblHeader.Text = strTempLabelCategory.ToUpper();

            Label lbltempdiv = item.FindControl("lbl_cb_all") as Label;
            lbltempdiv.Visible = true;
        }
    }
}

I have looked for something which can work within my code but no luck. And I am too invested in this code to now try Checkboxlist control (not sure if that would help anyway).
Any ideas? I thought I could use: http://www.dotnetcurry.com/ShowArticle.aspx?ID=77 code but not sure how I can make that work? If I can somehow make the select all checkbox tie to a tag and then look for all tag then may be the link’s code would help.
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:31:08+00:00Added an answer on June 1, 2026 at 3:31 pm
    1. Use HiddenFields to store CatID and DocID
    2. Use ASP.NET CheckBoxes instead of HTML-Inputs for both
    3. Handle the Check-All Checkbox’ CheckedChanged event:

    protected void CheckAllChanged(Object sender, EventArgs e)
    {
        CheckBox checkAll = (CheckBox)sender;
        DataListItem item = (DataListItem)checkAll.NamingContainer;
        HiddenField HiddenCatID = (HiddenField)item.FindControl("HiddenCatID");
        var catCheckBoxes = DataList1.Items.Cast<DataListItem>()
            .Where(li => ((HiddenField)li.FindControl("HiddenCatID")).Value == HiddenCatID.Value)
            .Select(li => li.FindControl("cb_docid"));
        foreach (CheckBox docCheckBox in catCheckBoxes)
        {
            docCheckBox.Checked = checkAll.Checked;
        }
    }
    

    on aspx:

    <asp:CheckBox runat="server" OnCheckedChanged="CheckAllChanged" AutoPostBack="true"  id="cb_selectall" />
    <asp:HiddenField ID="HiddenCatID" runat="server" Value='<%# Eval("CatID") %>' />
    <asp:HiddenField ID="HiddenDocID" runat="server" Value='<%# Eval("DocID") %>' />
    

    You also need to EnableViewState=true on the DataList to maintain checkbox state and enable checking/unchecking.


    Edit:

    Because you have problems to get it running, here’s a complete working sample page.

    Here are the required controls on aspx(note f.e. <tr runat="server" id="tr_category">):

    <ItemTemplate>
        <table cellpadding="0" cellspacing="0" id="tbl_data">
            <tr runat="server" id="tr_category">
                <td>
                    <asp:Label ID="lblHeader" runat="server" Font-Bold="True" Text='<%# Eval("categoryname") %>' Font-Underline="True"></asp:Label>
                    <asp:Label runat="server" ID="lbl_cb_all">Select All
                    <asp:CheckBox runat="server"  OnCheckedChanged="CheckAllChanged" AutoPostBack="true"  id="cb_selectall" />
                    </asp:Label>
                </td>
            </tr>
            <tr runat="server" id="tr_data">
                <td>
                    <asp:HiddenField ID="HiddenCatID" runat="server" Value='<%# Eval("CatID") %>' />
                    <asp:CheckBox runat="server" id="cb_docid" />
                    <asp:HyperLink ID="hpl_docfileencr" Text='<%# Eval("docfileencr") %>' NavigateUrl='<%# "~/PDFEncr/" + DataBinder.Eval(Container.DataItem, "docfileencr") %>'
                        Target="_blank" runat="server" />
                    <br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
    
    1. Ensure that the DataList is databound only if(!IsPostback), otherwise the checkbox selection will not be maintained:

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack) BindDataList();
      }
      
    2. I’m using ItemDataBound instead of PreRender which is important for the ViewState to be reloaded correctly. I’ve also simplified all:

      protected void DataList1_ItemDataBound(Object sender, DataListItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
          {
              var row  = (DataRowView)e.Item.DataItem;
              var view = row.DataView;
              var lastRow = e.Item.ItemIndex == 0 ? null : view[e.Item.ItemIndex-1];
              var tr_category = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("tr_category");
              var sameCategory = lastRow != null && (int)row["catid"] == (int)lastRow["catid"];
              tr_category.Visible = !sameCategory;
          }
      }
      
    3. The CheckAllChanged remains unchanged.

    This works as expected even if you select a single document-checkbox and select a category-checkbox afterwards which causes a postback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page where the user can select three different appointment days using
I have a jqGrid on a page and users can click a button to
I have a Page with many Comments. Many users can access this page and
I have created a page on my site where users can post directly to
I have a subscription link on a page that the logged in users can
I have a page where the user can edit various content using buttons and
I have an application where different users can log in via a single portal
let us say I have a list page of users and you can sort
I have a website which uses different style sheets from which users can choose
I have a page where a user can import data to the site. either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.