How can I edit properties of multiple objects in a relatively shorter way?

119 Views Asked by At

I'm changing visible states of many labels or TextBoxes depending on a choice from a RadioButtonList. As the page grows, these controls get longer and longer. I was thinking there must be a simpler and shorter way to do this but every solution I found led to longer pieces of code.

Here is an example of what I'm doing:

if (Tip == "Firma")
{
    fsFirma.Visible = true;
    txtGtip.Visible = false;
    lblGtip.Visible = false;
    lblFirmaGtip.Visible = false;
    txtFirmaGtip.Visible = false;
    lblFirmaInfo.Visible = true;
    lblGtipGrup.Visible = false;
    drpGtipGrup.Visible = false;
}

This type of a control happens for every 4 choices of the RadioButtonList.

6 out of 8 of these are set to false. Is there another syntax or method to assign false to them at once? Or is this the right way to do this? I think shorter code might not be best way all the time but still this guys look like they can be shortened a bit.

Edit: I forgot to mention I'm aware I can just set the default setting to false on asp code and manipulate needed ones as true but I'm asking to see other approaches too.

1

There are 1 best solutions below

0
On BEST ANSWER

use foreach statement to iterate through a collection of controls, like this:

if (Tip == "Firma")
{
    foreach (Control item in yourDiv.Controls)
     {
        item.Visible = false;
     }
    fsFirma.Visible = true;
    lblFirmaInfo.Visible = true;
}

Suppose your Div element is like this:

<div id="yourDiv" runat="server">
   <%--your controls--%>
</div>