0

This is my code for adding a check box progrmatically but it doesnt let me add an event onchecked

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

        CheckBox chk = new CheckBox();
        chk.EnableViewState = true;
        chk.Enabled = true;

        chk.ID = "chkb";
        DataRowView dr = (DataRowView)e.Row.DataItem;


        e.Row.Cells[0].Controls.Add(chk);

        e.Row.TableSection = TableRowSection.TableBody;
    }

when I try to add this:

chk.CheckedChanged += checkBox_CheckedChanged;
I get this error : 
"The name 'checkBox_CheckedChanged' does not exist in the current context",

even though I have already added this function:

   private void CheckBox_CheckedChanged(object sender, System.EventArgs e)
  {
   Response.Write("in check changed object");
  }
6
  • 2
    Capital letter? chk.CheckedChanged += CheckBox_CheckedChanged; Commented Aug 12, 2013 at 19:06
  • C# is Case Sensitive so the error is correct if your code is an exact copy. Commented Aug 12, 2013 at 19:06
  • 1
    Capital letter for CheckBox_CheckedChanged Commented Aug 12, 2013 at 19:06
  • All, please don't post answers in the comments. Comments are for asking for clarification on the question, not providing an answer. Commented Aug 12, 2013 at 19:06
  • 1
    @AdamRobinson I like to give people the benefit of the doubt and don't immediately assume they missed something trivial in case the code is an example they typed free-hand and not actually their running code. Commented Aug 12, 2013 at 19:10

2 Answers 2

4

C# is case-sensitive. Your function is named CheckBox_CheckedChanged, but you are attempting to attach an event handler for a function named checkBox_CheckedChanged (note the upper- vs. lowercase "c" at the beginning).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thank you , you were right I had to capitalize the first C. But now that I dont get an error, the function is not working, when I check or uncheck the box it never goes to the event handler. Can that be answered here or do I have to make a new question ?
@Dianacastillo: To keep things simple, please make a new question.
4

C# is case sensitive.

You should create this method:

private void checkBox_CheckedChanged(object sender, System.EventArgs e)
{
   Response.Write("in check changed object");
}

or, use your method and connect it like this:

chk.CheckedChanged += CheckBox_CheckedChanged;

A good way to avoid these kind of errors, is to first implement the handler (or at least define it), then let de IDE finish the typing for you.

1 Comment

checkBox_CheckedChanged to CheckBox_CheckedChanged

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.