Click here to Skip to main content
11,484,162 members (72,692 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET checkbox
Hi, this is my ASPX Page
<asp:Repeater runat="server" ID="rptChilds">
        <asp:CheckBox ID="chkc" runat="server" Text="" />
    <br />

i want to check that wich of the checkboxes check, for example by code as this
for (j = 0; j < NumberOfCheckboxes; j++)
                    CheckBox chk = (CheckBox)(rptChilds).FindControl("chkc");
                    if (chk != null)
                        if (chk.Checked)
                            //do something here

and this is the result:
Server Error in '/Project' Application.
Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 

Line 250:                    {
Line 251:                        CheckBox chk = (CheckBox)(rptChilds).FindControl("chkc");
Line 252:                        if (chk.Checked)
Line 253:                        {
Line 254:                            //your code here
Source File: f:\Documents\    \Projects\MahdZNU\Project\Pages\Image Gallery\showImages.aspx.cs    Line: 252 
Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   Pages_Image_Gallery_showImages.insertImageIntoGallery(Object sender, EventArgs e) in f:\Documents\                  \Projects\MahdZNU\Project\Pages\Image Gallery\showImages.aspx.cs:252
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +116
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +101
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642898
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Posted 16-Jan-13 21:14pm
Edited 17-Jan-13 2:49am
Sergey Alexandrovich Kryukov at 17-Jan-13 2:17am
OK, you can check it; you got my approval. :-)
Any questions? :-)
Shanu2rick at 17-Jan-13 2:20am
Print the value somewhere and see for yourself.
shajarian_lover at 17-Jan-13 2:23am
when I execute this code, it's get into else block (//error)
what can I do?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this:
foreach (RepeaterItem item in rptChilds.Items)
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        CheckBox chk = (CheckBox)(rptChilds).FindControl("chkc");
        if (chk.Checked)
            //your code here
            //your code here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 210
1 Frankie-C 155
2 Raje_ 155
3 OriginalGriff 147
4 Maciej Los 110
0 Sergey Alexandrovich Kryukov 8,489
1 OriginalGriff 7,943
2 Sascha Lefèvre 3,114
3 Maciej Los 2,581
4 Richard Deeming 2,370

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100