Click here to Skip to main content
15,886,812 members

How to Control that wich of the checkboxes checked

shajarian_lover asked:

Open original thread
Hi, this is my ASPX Page
ASP.NET
<asp:Repeater runat="server" ID="rptChilds">
    <ItemTemplate>
        <asp:CheckBox ID="chkc" runat="server" Text="" />
    <br />
    </ItemTemplate>
</asp:Repeater>


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


and this is the result:
HTML
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
Tags: ASP.NET, Checkbox

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900