1

I have a gridview , in a column of the gridview i have 2 checkbox with different ids So i do a foreach loop after i click a button ( update button ) to loop the rows and check if the checkbox of that particular row is checked , then i want to update all rows together after clicking a button

I populate the gridview in Page_Load() , the foreach loop goes the correct number of loop according to the number of Rows but it won't go into my If statement on checking if the checkbox is checked

Here are my codes :

    protected void btnUpdate_Click(object sender, EventArgs e)
{
    int count = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        count++;
        if (((CheckBox)row.FindControl("showBbtn")).Checked & ((CheckBox)row.FindControl("showCbtn")).Checked) // if 2 buttons are checked show error popout 
        {
            Response.Write(@"<script language='javascript'>alert('You can only select Yes or No')</script>");
        }
        else
        {
            if (((CheckBox)row.FindControl("showCbtn")).Checked) // Won't enter
            {
                //Do something here.
            }
            if (((CheckBox)row.FindControl("showBbtn")).Checked) // Won't enter
            {
                // Do something here.
            }

        }


    }

    Response.Write(count);
}

And this is how i populate my gridview :

 protected void Page_Load(object sender, EventArgs e)
{
    BindQuestion(); // Foreach loop count is according to the number of rows
    if (!Page.IsPostBack)
    {
       // BindQuestion(); // Can enter the if loop but the foreach loop count is only 1.
    }
}

My aspx :

 <asp:TemplateField HeaderText="Display">
            <ItemTemplate>
                <asp:CheckBox ID="showCbtn" runat="server" Text = "Yes"  />
                <br />
                <asp:CheckBox ID="showBbtn" runat="server" Text = "No"  />
            </ItemTemplate>
        </asp:TemplateField>
6
  • I think you should use only one checkbox. If it's checked then update row otherwise not. Commented Feb 6, 2014 at 6:13
  • I used 2 checkbox because i want to them to select Yes or No , then i'll update Yes or No into database of that particular record , the problem now is , it won't enter the if statement , i want to update the whole gridview Commented Feb 6, 2014 at 6:16
  • 1
    if you want end user to display Yes and No options and user should be able to select any one of them then you can use radio buttons. Commented Feb 6, 2014 at 6:31
  • The problem is that it will only update a row each time instead of updating every row once the update button is clicked Commented Feb 6, 2014 at 6:38
  • Means If any checkbox is checked, based on that checked value you need to update whole GridView records? Commented Feb 6, 2014 at 6:47

4 Answers 4

2

There is problem in your If condition. You have given incorrect & sign, it should be two && sign.Replace

if (((CheckBox)row.FindControl("showBbtn")).Checked & ((CheckBox)row.FindControl("showCbtn")).Checked)  

with

if (((CheckBox)row.FindControl("showBbtn")).Checked && ((CheckBox)row.FindControl("showCbtn")).Checked) 

And also add the BindQuestion() inside ispostback.

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

5 Comments

i have tried , it doesn't affect the outcome , its still the same and if i put the BindQuestion() inside ispostback , the foreach loop will only run once due to the postback , it won't enter the 2 IF loop inside the else {} if i were to put it ouside of the ispostback
if i put inside the ispostback , it will enter the IF loop but will only run once , means i can only update 1 row at a time , instead of updating every row once i clicked the update button
Hi @Raghubar I think solution for his problem is using XML , we can send all the updated values at one instance. what do you say ?
Yes, if use XML then for All update it need to connect database once.
@user2601570 what do u want to do and what is your actual problem ?
1

This may help you

 foreach (GridViewRow gr in grdCreateDues.Rows)
            {
                CheckBox chkC = gr.FindControl("showCbtn") as CheckBox; 
                CheckBox chkB = gr.FindControl("showBbtn") as CheckBox; 

                GridViewRow Row = ((GridViewRow)chk.Parent.Parent);

                if (chkC.Checked)
                {
                    // update tblGridtable set xzy = @xyz  
                }
                else if (chk.B.Checked)
                {
                     // update tblGridtable set abc = @abc     
                }

            }

Comments

0

This method is also working

var checkBox = (CheckBox)item.FindControl("ckbActive");

1 Comment

Avoid code-only answers. Consider to provide more detailed explanation of the solution.
0

Try using this:-

CheckBox chbx = GridView1.HeaderRow.FindControl("CheckBox1") as CheckBox;

if (chbx != null && chbx.Checked)
{
Condition
}

Comments

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.