0

So I am adding check boxes to an HTML page. However, when I try to reference my CheckBox2, I get the error. Here is the checkbox in my html. Default.aspx:

<asp:CheckBox ID="CheckBox1" runat="server" Text="Yes" OnCheckedChanged="CheckBox1_CheckChanged" />

This is no problem at the moment, however the error is thrown here in my "C#" Default.aspx.cs:

protected void CheckBox1_CheckChanged(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
        CheckBox2.Checked = false;
        CheckBox3.Checked = false;
        CheckBox4.Checked = false;
        CheckBox5.Checked = false;

    }

Apparently the check boxes dont exist in the current context, also, double clicking them did not create the event handler, I had to do so myself. Ive looked at many other similar questions and the answers helped everyone else but still none of them have fixed this problem for me

SJ10
  • 315
  • 1
  • 4
  • 12
  • Have you added Checkbox2, Checkbox3, etc.. to the Html page with runat="server" if not, that's your issue. Also, if your Checkbox2 is the one in your example, your ID is Checkbox1, which is a typo – Ryan Wilson May 21 '18 at 18:43
  • All checkboxes are using runat="server", also, the CheckBox1 example in html I have posted is the wrong example to explain the CheckBox2 problem, ill comment the right bit of code – SJ10 May 21 '18 at 19:04
  • Also, this is only A problem with THIS PARTICULAR PROJECT. Like, if I start a new project, the same code that I have here works. So Im really confused about that – SJ10 May 21 '18 at 19:13
  • Have you checked inside the designer.cs file for the new controls? – Ryan Wilson May 21 '18 at 19:24
  • I dont even have that file unfortunately – SJ10 May 21 '18 at 19:28
  • I would say that is part of the problem then, lol. Have a look at this other Stack Overflow post, it has many different reasons for the behavior you listed and possible solutions (https://stackoverflow.com/questions/6180509/asp-net-controls-are-not-accessible-in-code-behind) – Ryan Wilson May 21 '18 at 19:29
  • Alright, will do, thanks! – SJ10 May 21 '18 at 19:50

1 Answers1

0

Since I'm not allowed to comment I'll post this as an answer for now. Feel free to correct this.

You should check the top of your *.ascx file and make sure it is referencing the correct codefile. For instance, in the top of the file I'm working on, I have a line at the top of the file that says:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/Default.aspx.cs" Inherits="Default.MethodWithCheckBox2CodeInIt" %>

In this case, Upload.ascs.cs being the codefile with my controls in it. Make sure your "CodeFile" property in that line is exactly right.

EDIT: Again, I'd rather ask OP about this in the comments section since we need to see more of their code to know if this is the issue or not. Since I don't have 50 reputation here I have posted it as an answer.

Marley Stacy
  • 100
  • 12
  • My project is using .asPx files, not ascx files. As far as that line of code, Ive seen this sort of method as an answer before, honestly I dont even have that line of code, but every time Ive tried to use this method I get the same error. Ive tried again, same error. This is the line of code ive added to the top of my html: -----%@ Control Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>----- what else would you like to see that would make answering my question easier? – SJ10 May 21 '18 at 18:59
  • In my case, I have to specify the full path to the codefile, using the name alone gives me the same error you mentioned. Let me look for some of my ASPX examples, one sec – Marley Stacy May 21 '18 at 19:47
  • Try putting it like this "CodeFile="~/Default.aspx.cs" – Marley Stacy May 21 '18 at 19:48
  • Actually here's how the entire line should be in an aspx file: Page Language="C#" MasterPageFile="~/Admin.master" AutoEventWireup="true" CodeBehind="~/Default.aspx.cs" Inherits="Default" – Marley Stacy May 21 '18 at 19:49
  • I realize what you mean, but the part that is confusing is that this code works fine in another project, Its like the webpage I was editing isnt compatible with the adjustments im making – SJ10 May 21 '18 at 19:49
  • No that makes perfect sense, especially if you copy pasted any code, it may be Visual Studio itself not pairing the files up properly in the environment rather than an error at runtime. – Marley Stacy May 21 '18 at 19:50
  • One last thing to check, where you have "Inherits", you want to specify the exact methods you're importing. Gonna update the answer to what I think your whole line should be. – Marley Stacy May 21 '18 at 19:52
  • Just noticed the commenter in the above section (sorry I can't comment) mentioned the designer file. Worth looking into, but if you're using Visual Studio it should have created one for you, or else may be using only default controls removing the need to modify it. Plus you said it worked in another file in the project so that's not the problem I don't think. – Marley Stacy May 21 '18 at 19:57
  • I meant the code works in another project that was blank, I appreciate your help but at this point I have realized I cant just edit the webpage I was originally working on, It wouldnt be the solution I was looking for after all, for my purposes I must start a new website. Sorry that im dropping it like this, I really do appreciate your help but I cant keep working on this project. – SJ10 May 21 '18 at 20:22
  • No worries! Glad you found the right direction for your solution. – Marley Stacy May 22 '18 at 13:44
  • Thank you! It requires me to write more to post a comment – SJ10 May 22 '18 at 14:20