1

I have nested Gridview and the last Gridview has Checkbox.

the condition in checking the Checkbox whether it is checked or unchecked

foreach (GridViewRow gvr in GridView1.Rows)
{
       GridView GridView2= gvr.FindControl("GridView2") as GridView;
       foreach (GridViewRow gvr2 in GridView2.Rows)
       {
           GridView GridView3= gvr2.FindControl("GridView3") as GridView;
           foreach (GridViewRow gvr3 in GridView3.Rows)
           {
               if(((CheckBox)gvr3.FindControl("chk1")).Checked)
               {
                    string txt = txtKeyboard.text;
               }
           }
       }
}

even though it is unchecked it will go though the condition

6
  • post more code for identifying problem Commented Mar 7, 2014 at 7:19
  • are you sure you are referencing the right gridrow and right checkbox? there is no reason why that wont work unless you are checking it in code somewhere else before this loop gets executed or you are referencing the wrong checkbox. Commented Mar 7, 2014 at 7:53
  • Yes Im definitely sure that i am referencing the right checkbox from the gridview Commented Mar 7, 2014 at 8:11
  • Please post the whole class. We can't help you if you don't do that. Commented Mar 7, 2014 at 8:24
  • Where did you wrote this code? I mean which event ? Commented Mar 7, 2014 at 8:35

2 Answers 2

1
if(((CheckBox)gvr3.FindControl("chk1")).Checked)
{
     string txt = txtKeyboard.text;
}

instead of above code just try once

  CheckBox chk1 = gvr3.Cells[0].Controls[0] as CheckBox;
  if(chk1.checked==true)
  {
      string txt = txtKeyboard.text;
  }

here you need to set cell value and control value inplace of 0

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

4 Comments

Also returning the uncheck buttons to true
@Registered_User012345 i clearey mentioned here that you need to set value of cells(order of checkbox from starting of Gridview3) and control in place of 0
Sorry I double check the control placement and corrected it. The code also returning true even though the checkboxs are unchecked
@Registered_User012345 PLEASE PUT YOUR ASPX CODE OR COME TO THIS ROOM
0

How you bind data to "GridView2" control? if you called some methods like "GridView2_DataBind" , after postback data has be processed, the state of girdview will be reset.

for example,

 protected void Page_Load(object sender, EventArgs e)
 {
          if (!this.IsPostBack){ 
              GridView2_DataBind() // you shouldn't call databind method when postback
          }
 }

1 Comment

Nope, Im not binding the grids in postback

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.