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 8186533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:14:25+00:00 2026-06-07T02:14:25+00:00

I have a custom GridView Control where I grab data from the database to

  • 0

I have a custom GridView Control where I grab data from the database to populate the control. On the page I have also created a HeaderTemplate checkbox control and an ItemTemplate checkbox control:

<nm:ContactGridViewControl runat="server" ID="grdContacts">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox runat="server" AutoPostBack="true" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </nm:ContactGridViewControl>

I populate the GridView as follows in the OnInit event. I Chose not to repopulate on every postback because it was slowing down the app.

protected override void OnInit(EventArgs e)
    {
        this.RowDataBound += new GridViewRowEventHandler(ContactGridViewControl_RowDataBound);
        this.RowCreated += new GridViewRowEventHandler(ContactGridViewControl_RowCreated);

        if (!Page.IsPostBack)
        {
            List<EnquiryItem> contactList = new List<EnquiryItem>();
            DataTable list = new DataTable();

            if (SessionManager.LoginState != null)
            {
                contactList = SiteDataLayerHandler.GetContactList(SessionManager.LoginState.UserID);
            }

            if (contactList != null)
            {
                list.Columns.Add("LeadID");
                list.Columns.Add("Name");
                list.Columns.Add("Email Address");

                foreach (EnquiryItem item in contactList)
                {
                    DataRow row = list.NewRow();

                    row["LeadID"] = item.LeadID;
                    row["Name"] = string.Format("{0} {1}", item.FirstName.ToCapitalize(), item.LastName.ToCapitalize());
                    row["Email Address"] = item.EmailAddress;

                    list.Rows.Add(row);

                }

                this.DataSource = list;
                this.DataBind();
            }
        }

        base.OnInit(e);
    }

In order to keep all code associated with the control in one place I have added a ‘CheckedChanged’ event dynamically on ‘OnRowDataBound’ This is just for the Checkbox in the HeaderTemplate. The Reason is so I can use this checkbox as a ‘Select/Deselect All Rows’:

protected void ContactGridViewControl_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1) // Check if row is Header row
        {
            CheckBox chk = e.Row.GetAllControls().OfType<CheckBox>().FirstOrDefault();

            if (chk != null)
            {
                chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
            }
        }
    }

I then have the event code on the same page like so:

protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        bool isChecked = ((CheckBox)sender).Checked;

        foreach (GridViewRow row in this.Rows)
        {
            CheckBox chkBox = row.Cells[0].Controls[0] as CheckBox;

            if (chkBox != null)
            {
                chkBox.Checked = isChecked;
            }
        }
    }

This is where the problems start. My event never gets hit! However, the checkbox does postback.

  • 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-07T02:14:28+00:00Added an answer on June 7, 2026 at 2:14 am

    Ok so the answer is this. I needed to assign the CheckedChanged event on the ‘OnRowCreated’ event instead of ‘OnRowDataBound’

    protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
            {
                CheckBox chk = e.Row.GetAllControls().OfType<CheckBox>().FirstOrDefault();
    
                if (chk != null)
                {
                    chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
                }
            }
            base.OnRowCreated(e);
        }
    

    This way the event hits the method everytime

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

Sidebar

Related Questions

I have developed an asp.net control that inherits from the gridview and its called
I have custom CSS for checkbox control into my web form, which is linked
I have a custom gridview control that extends the standard asp.net gridview control. The
I have a custom control that is based off the gridview control located at:
I have a TemplateField that is dynamically added to a custom GridView. void ITemplate.InstantiateIn(System.Web.UI.Control
I have a custom control that is derived from ListView . This list view
I have added a GridView control on my ASP.net webpage and data bound it
I have asp.net page where I am using a gridView with custom paging and
I have a custom GridView that will show 6 rows of data as a
I have a custom GridView like control made of a Grid panel with several

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.