1

I want to check a checkbox after finding that it is checked in database. I'm trying this:

string chkboxbit = String.Empty;
CheckBox cb = new CheckBox();
chkboxbit = "ct100$ContentPlaceHolder1_chk" + item + "" + i;
cb = (CheckBox)Page.FindControl(chkboxbit.ToString());
cb.Checked = false;

But I'm getting cb value null on debugging.

7
  • 1
    You sure that control is in your page? Jun 25, 2014 at 3:33
  • 1
    is value of chkboxbit correct after concatenating with item & i?? Jun 25, 2014 at 3:38
  • Please edit your post and add the HTML tag as well as the value of chkboxbit post concatenation. Jun 25, 2014 at 3:42
  • I think you're going about finding your control the wrong way. What are you trying to do?
    – crthompson
    Jun 25, 2014 at 3:42
  • 1
    possible duplicate stackoverflow.com/questions/9830198/find-control-in-page
    – Kyojimaru
    Jun 25, 2014 at 3:50

2 Answers 2

1

I got it... and it works fine...

chkboxbit = "chk" + item + "" + i;
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
cb = ((CheckBox)cph.FindControl(chkboxbit));
if (cb != null)
    cb.Checked = false;

Thnks to all for their effort. and Thanks Kyojimaru.

0

When you are doing this:

chkboxbit = "ct100$ContentPlaceHolder1_chk" + item + "" + i;

It appears you are mistaking the ID in the HTML for the ID that you give in the .ASPX page.

ct1000 usually come from the master page. I would bet you only need to do this:

chkboxbit = "ContentPlaceHolder1_chk" + item + i.ToString();

And your control should be found.

1
  • Hey Adama, it is still not working. while ruuning page, after inspecting element, id is same and name becomes ct100$ContentPlaceHolder1_chkProject4. Here is my aspx code : <asp:CheckBox ID="chkProject4" CssClass="accesschkbox" runat="server" ClientIDMode = "Static"
    – Mayank
    Jun 25, 2014 at 4:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.