0

hello to all,
i have write a code in asp.net to show and hide the div.
according to the value in the query string.
here is my code:

if (Request.QueryString["divid"] != null)
        {
            divid =Request.QueryString["divid"].ToString().Trim();
            if (divid == "watercalc")
            {
                watercalc.Style["display"] = "block";
                pregnancycalc.Style["display"] = "none";
                heartbeatcalc.Style["display"] = "none";
                fatcalc.Style["display"] = "none";
                heightcalc.Style["display"] = "none";

            }
            else if (divid == "pregnancycalc")
            {
                watercalc.Style["display"] = "none";
                pregnancycalc.Style["display"] = "block";
                heartbeatcalc.Style["display"] = "none";
                fatcalc.Style["display"] = "none";
                heightcalc.Style["display"] = "none";

            }
            else if (divid == "heartbeatcalc")
            {
                watercalc.Style["display"] = "none";
                pregnancycalc.Style["display"] = "none";
                heartbeatcalc.Style["display"] = "block";
                fatcalc.Style["display"] = "none";
                heightcalc.Style["display"] = "none";

            }
            else if (divid == "heightcalc")
            {
                watercalc.Style["display"] = "none";
                pregnancycalc.Style["display"] = "none";
                heartbeatcalc.Style["display"] = "none";
                fatcalc.Style["display"] = "none";
                heightcalc.Style["display"] = "block";

            }
            else if (divid == "fatcalc")
            {
                watercalc.Style["display"] = "none";
                pregnancycalc.Style["display"] = "none";
                heartbeatcalc.Style["display"] = "none";
                fatcalc.Style["display"] = "block";
                heightcalc.Style["display"] = "none";

            }
        }

now, i want to compress my code.
i know i can do it with the use of array but i am unable to do this.
anyone could help me?please......

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by croker10
0

Are you trying to simplify your code? If that is the case, I would recommend something along the lines of setting all the values to a default of "none" as that is most often the case, and changing only the values that need it. See below:

watercalc.Style["display"] = "none";
                pregnancycalc.Style["display"] = "none";
                heartbeatcalc.Style["display"] = "none";
                fatcalc.Style["display"] = "none";
                heightcalc.Style["display"] = "none";

and then doing the

divid =Request.QueryString["divid"].ToString().Trim();

after that you can have the if else statements followed by one line setting the necessary item to "block", removing the repetition and extra brackets should cut down the lines of code by half or so.

Not sure if this is what you were actually looking for, but that's how I understood the question....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.