简体   繁体   中英

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM