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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:37:37+00:00 2026-05-13T15:37:37+00:00

I have a custom control that is based off the gridview control located at:

  • 0

I have a custom control that is based off the gridview control located at: here

The control is basically a gridview that automatically creates a column of checkboxes that can be used to “check” individual rows in the gridview.
During the gridview’s “CreateColumns” event, the “checkboxcolumn” is created dynamically. The checkboxcolumn also contains another checkbox in the header that is used to “select/deselect all” checkboxes in the column.

Since the gridview does not automatically remember the state of the checkboxes in the checkboxcolumn on postback, I added a method to the control called “SaveCheckBoxState” which stores the indexes of the checked rows in Viewstate, and then I modified the “OnRowDataBound” event to check the Viewstate and reset the checkboxes based on the Viewstate.

I then added a call to “SaveCheckBoxState” in the gridview’s OnSorting and OnPageIndexChanging events. This works great so long as I’m sorting or changing pages.

However, I need it to update the viewstate everytime someone clicks or unclicks one of the checkboxes. At this time, the checkboxes are rendered with an onclick event that calls some javascript to highlight the row, or in the case of the checkbox in the header, to select/deselect all checkboxes.

I need to call the “SaveCheckBoxState” method from the javascript used by the customcontrol, or I need to find a way to modify viewstate from javascript and perform the same action as “SaveCheckBoxState”.

I’ve tried adding the “SaveCheckBoxState” to the onclick event declaration in the checkboxes, but when run, it simply tells me that the method is undefined. It doesn’t exist in the parent page, and I don’t think I should have to make an event for the parent page to pass the click to. It seems to me this should be all self contained within the custom control.

Does anyone know how I can acheive this?

Here is the code for the gridview OnPreRender event where the onclick event of the checkbox is set:

protected override void OnPreRender(EventArgs e)
        {
            // Do as usual
            base.OnPreRender(e);

            // Adjust each data row
            foreach (GridViewRow r in Rows)
            {
                // Get the appropriate style object for the row
                TableItemStyle style = GetRowStyleFromState(r.RowState);

                // Retrieve the reference to the checkbox
                CheckBox cb = (CheckBox)r.FindControl(InputCheckBoxField.CheckBoxID);

                // Build the ID of the checkbox in the header
                string headerCheckBoxID = String.Format(CheckBoxColumHeaderID, ClientID);

                // Add script code to enable selection
                cb.Attributes["onclick"] = String.Format("ApplyStyle(this, '{0}', '{1}', '{2}')",
                            SelectedRowStyle.CssClass,
                            style.CssClass,
                            headerCheckBoxID);

                // Update the style of the checkbox if checked
                if (cb.Checked)
                {
                    r.BackColor = SelectedRowStyle.BackColor;
                    r.ForeColor = SelectedRowStyle.ForeColor;
                    r.Font.Bold = SelectedRowStyle.Font.Bold;
                }
                else
                {
                    r.BackColor = style.BackColor;
                    r.ForeColor = style.ForeColor;
                    r.Font.Bold = style.Font.Bold;
                }
            }
        }
  • 1 1 Answer
  • 2 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-05-13T15:37:37+00:00Added an answer on May 13, 2026 at 3:37 pm

    You should have your custom GridView control subscribe to the CheckChanged event of the checkboxes. It should call its SaveCheckBoxState method on its own. That method should be private.

    Parent and child controls should only communicate via properties and events, never through methods and fields.

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

Sidebar

Related Questions

I have a custom control that inherits from UserControl , and I can't get
I have a custom control that can be added multiple times to a form.
Basically, I have created a custom control that uses an UpdatePanel, and as I
I have created a custom control (based on a panel) in wxPython that provides
Here's the situation: I have a custom TextBox control that contains multiple other TextBox
I have a custom user control (ascx) that contains a textbox and a Javascript-based
I have a Custom Control that has multiple textbox fields and a checkbox contained
I have a custom control that needs a minor adjustment when running on XP
I have a custom control that displays a tryAgain button when the control is
I have a custom control that inherits from .NET's CompositeControl class. This control overrides

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.