Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 17th, 2014, 10:21 AM
Authorized User
Points: 121, Level: 2
Points: 121, Level: 2 Points: 121, Level: 2 Points: 121, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Checkbox in GridView

Hi all, hope in your help.

If select in GV a check box from 1 row then you want all other rows in the GridView to be disabled leaving the current row enabled...

I tried this code but not working ... can you help me?

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow GridView1 = (GridViewRow)chkTest.NamingContainer;

        if (chkTest.Checked)
           GridView1.BackColor = System.Drawing.Color.Yellow;

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            DropDownList DDL = (DropDownList)e.Row.FindControl("DDL");

            foreach (GridViewRow gvr in GridView1.Rows)
                if (((CheckBox)gvr.FindControl("chkSelect")).Checked)
                    DDL.Enabled = false;
                    DDL.Enabled = true;
Reply With Quote
  #2 (permalink)  
Old April 25th, 2014, 06:58 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts

Have you stepped through it to find out why it not working. I see one thing off the top that I know has frustrated me time and time again is that the find control method does not recursively search for controls and does not find controls that are not a direct a child of the control your calling the method on. Since your working with check box i am assuming its a templated gv so you might check to see if you are actually finding the check box during execution. If your not then write a recursive search for control.

public Control FindControl( Control cntrl, string controlID )
    Control rVal = null; 

     if( cntrl.ID == controlID )
         rVal = cntrl;
          // we do the recursive depth search on all child control until we find it
          foreach( var c in cntrl.Controls )
                rVal = FindControlRecursive( cntrl, controlID );
                if( rVal != null )
                     break; // found quit looking

    return rVal;
I also notice that you try to find a drop down for the current row that was bound, and then iterate through the entire gv looking at each row for a check box, toggling the current bound rows drop down on and off. Is this what you intended or did you intend on enabling the current rows drop down base on what that rows check box was?

You can do this with the recursive search defined above
var ddl = FindControl(e.Row,"DDL");
var chbx = FindControl(e.Row,"chkSelect");
ddl.Enabled  = chkbx.Checked;

Last edited by mmorgan30; February 26th, 2015 at 09:43 PM.. Reason: added search code, update findcontrol code
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
can't get checkbox value from Gridview akshay144 ASP.NET 3.5 Basics 4 March 31st, 2011 02:52 AM
Gridview and Checkbox krasnokojiy ASP.NET 2.0 Basics 14 February 28th, 2008 12:56 PM
Checkbox in Gridview yukijocelyn ASP.NET 2.0 Basics 0 September 24th, 2007 12:32 AM
GridView Checkbox ItemTemplate angelicagm ASP.NET 2.0 Professional 3 May 31st, 2007 04:44 AM
Checkbox in GridView aliirfan84 ASP.NET 2.0 Professional 1 May 15th, 2007 03:59 PM

All times are GMT -4. The time now is 09:45 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.