0

hi i would like to know how can i check if any of the reviewBtn is visible in the gridview and if ANY reviewBtn is present, btn_reviewAll will be visible.

currently the code below only shows the btn_reviewAll when ALL reviewBtn is visible. pls advise thanks!

foreach (GridViewRow row in GridViewReview.Rows)
{
    Control reviewBtn = row.FindControl("ButtonReview") as Button;

    if (reviewBtn.Visible == true)
    {
       btn_reviewAll.Visible = true;
    }
    else
    {
       btn_reviewAll.Visible = false;
    }
}
2
  • Add a breakpoint at reviewBtn.Visible and view in Quickwatch what value you are getting for this control. Commented Sep 29, 2015 at 6:37
  • @Suprabhat the value is a Text = "Review" Commented Sep 29, 2015 at 6:44

2 Answers 2

4

change your code like this

foreach (GridViewRow row in GridViewReview.Rows)
{
    Control reviewBtn = row.FindControl("ButtonReview") as Button;

    if (reviewBtn.Visible == true)
    {
         btn_reviewAll.Visible = true;
         break;
    }
    else
    {
       btn_reviewAll.Visible = false;
    }
}

what it does is when one reviewBtn is visible it will set btn_reviewAll to visible and break out the foreach loop

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

Comments

1

@Shreesha's answer is absolutely correct, you can also do with less code using LINQ like this:-

if (GridViewReview.Rows.OfType<GridViewRow>()
                  .Any(b => ((Button)b.FindControl("ButtonReview")).Visible))
      btn_reviewAll.Visible = true;
else
      btn_reviewAll.Visible = false;

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.