p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 12th, 2009, 11:41 AM
Authorized User
Points: 96, Level: 1
Points: 96, Level: 1 Points: 96, Level: 1 Points: 96, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2006
Location: london, , United Kingdom.
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
Send a message via MSN to akshay144
Post can't get checkbox value from Gridview

Hi All

I am populating a grid at runtime/on-the-fly which is fine.

But when I want to get a value of a check-box in the grid I am getting error.
I can't use the findcontrol as it doesn't exist for any of the controls.

Please help me.



CodeBehind:-

Code:
protectedvoid GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool att = false;
GridViewRow gvrow = GridView1.Rows[e.RowIndex];
CheckBox chk_SelectCheckbox = (CheckBox)gvrow.Cells[4].Controls[1]; //checkbox to capture chkbox selection
}
Error screenshot





Server Error in '/Event' Application.

Specified argument was out of the range of valid values.
Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:

Line 135: bool att = false;Line 136: GridViewRow gvrow = GridView1.Rows[e.RowIndex];Line 137: CheckBox chk_SelectCheckbox = (CheckBox)gvrow.Cells[4].Controls[1]; //checkbox to capture chkbox selectionLine 138: Line 139:
Source File: c:\Documents\Visual Studio 2008\Event\UpdateAttendance.aspx.cs Line: 137

Stack Trace:

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.Parameter name: index] System.Web.UI.ControlCollection.get_Item(Int32 index) +8668910 UpdateAttendance.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\Users\Mayur\Desktop\Data\urvashi\Documents\Visu al Studio 2008\IndianSpeedDating\UpdateAttendance.aspx.cs:13 7 System.Web.UI.WebControls.GridView.OnRowUpdating(G ridViewUpdateEventArgs e) +133 System.Web.UI.WebControls.GridView.HandleUpdate(Gr idViewRow row, Int32 rowIndex, Boolean causesValidation) +720 System.Web.UI.WebControls.GridView.HandleEvent(Eve ntArgs e, Boolean causesValidation, String validationGroup) +704 System.Web.UI.WebControls.GridView.OnBubbleEvent(O bject source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEven t(Object source, EventArgs e) +123 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e) +118 System.Web.UI.WebControls.LinkButton.RaisePostBack Event(String eventArgument) +135 System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old August 12th, 2009, 12:38 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,250, Level: 27
Points: 4,250, Level: 27 Points: 4,250, Level: 27 Points: 4,250, Level: 27
Activity: 30%
Activity: 30% Activity: 30% Activity: 30%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 807
Thanks: 12
Thanked 140 Times in 140 Posts
Default

Don't forget these indexes are zero-based.

If you want to target the 4th cell in the row, you'd use Cells[3].

If the checkbox is the only control in that cell, you'd use Controls[0].
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to Lee Dumond For This Useful Post:
akshay144 (August 13th, 2009)
  #3 (permalink)  
Old August 21st, 2009, 01:45 AM
Authorized User
Points: 183, Level: 3
Points: 183, Level: 3 Points: 183, Level: 3 Points: 183, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: singapore, singapore, Singapore.
Posts: 49
Thanks: 0
Thanked 3 Times in 3 Posts
Default

i would i advise you to use findControl of the respective row. This way even if you add some more controls in the future it wont break at runtime

gvrow.Cells[4].FindControl()
__________________
Pon Saravanan
VB Knowledgebase, Car PC,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to pons_saravanan For This Useful Post:
akshay144 (August 24th, 2009)
Reply


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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
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 01:32 AM
GridView Checkbox ItemTemplate angelicagm ASP.NET 2.0 Professional 3 May 31st, 2007 05:44 AM
Checkbox in GridView aliirfan84 ASP.NET 2.0 Professional 1 May 15th, 2007 04:59 PM
Gridview checkbox problems pauliehaha ASP.NET 2.0 Professional 1 May 3rd, 2007 05:14 AM



All times are GMT -4. The time now is 05:10 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc