1

I have 30 checkboxes in my web form, and i want to store the status of checkboxes in a array of bool. now since asp.net doesn't accept using the "checked" status of a checkbox with a variable like "i" ,problem is i don't know how to perform the code!

i want something like this:

bool[] array = new bool[30];
for (int i = 0; i < 30; i++)
   {
      array[i] = CheckBox(i).Checked ;
   }

when you run the above code you'll get this error:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0118: 'System.Web.UI.WebControls.CheckBox' is a 'type' but is used like a 'variable'

Source Error:

Line 109:        for (int i = 0; i < 30; i++)
Line 110:        {
Line 111:            array[i] = CheckBox(i).Checked;
Line 112:        }
Line 113:  

I exactly want to know how can i use IDs of checkboxes with a variable, like:

i = 15;
CheckBox(i).Checked

instead of:

CheckBox15.Checked

ASP.net 4 and c# Thank you Guys.

1 Answer 1

1

try this:

bool[] array = new bool[30];
for (int i = 0; i < 30; i++)
{
   array[i] = ((CheckBox)Page.FindControl("CheckBox" + i.ToString)).Checked;
}

UPDATE:

I just created a new project just to test if the codes are working, and it worked. really. :|

<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CheckBox ID="CheckBox2" runat="server" />
        <asp:CheckBox ID="CheckBox3" runat="server" />
        <asp:CheckBox ID="CheckBox4" runat="server" />
        <asp:CheckBox ID="CheckBox5" runat="server" />
        <asp:CheckBox ID="CheckBox6" runat="server" />
        <asp:CheckBox ID="CheckBox7" runat="server" />
        <asp:CheckBox ID="CheckBox8" runat="server" />
        <asp:CheckBox ID="CheckBox9" runat="server" />
        <asp:CheckBox ID="CheckBox10" runat="server" />
    </div>
    </form>
</body>

code-behind:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bool[] array = new bool[10];
        for (int i = 1; i <= 10; i++)
        {
            array[i] = ((CheckBox)Page.FindControl("CheckBox" + i.ToString())).Checked;
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It came With Error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 111: array[i] = ((CheckBox)Page.FindControl("CheckBox" + i.ToString())).Checked;" But thanks @vhinn terrible
Do you have a Checkbox whose ID is "CheckBox0"? if none, of course it will return NULL as it didnt see a control like that. change your for loop to start with 1 and not 0!
It's not because of that. I did what you told ans the result was the same, I also did: bool con = ((CheckBox)Page.FindControl("CheckBox10")).Checked; and it was the same. you'll wonder if i say i also tried this: string str = ((CheckBox)Page.FindControl("CheckBox10")).ID.ToString(); and result was just the same. i think it can't fine the specific control with this method at all!
Well thank you Vhinn, It worked. But with a tiny change.bool[] array = new bool[11]; That's all. thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.