Control visibiliy based on DropDown selection

I have a dropdownlist (DropDownList2), a label (Label1), and a textbox (TextBox7). When the selection in DropDownList2 is "Prop" TextBox7 and Label1 need to be visible. When the selection in DropDownList2 is anything else, TextBox7 and Label1 need to not be visible.
Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
One way is setting AutoPostBack=true like below:

<asp:DropDownList ID="DropDownList2" runat="server"
           onselectedindexchanged="DropDownList2_SelectedIndexChanged">
 </asp:DropDownList>

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
           if(DropDownList2.SelectedValue=="Prop")
{
//make controls invisible
}
else
{
 // make controls Visible
}

}
13598Commented:
if (DropDownList2.SelectedValue!="Prop") TextBox7.style.display='none';
Mike MillerSoftware EngineerAuthor Commented:
Here is what I have so far. Will this work? How do I call it?
</script>
    <script language="JavaScript" type="text/javascript">
        function invis2() {
            if (DropDownList2.SelectedValue != "Prop") {
             document.getElementById('TextBox7').style.visibility = 'Hidden';
             document.getElementById('Label1').style.visibility = 'Hidden';
        }
            else {
            document.getElementById('TextBox7').style.visibility = 'Visible';
            document.getElementById('Label1').style.visibility = 'Visible';
        }
</script>

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

13598Commented:
You can try this:
<asp:DropDownList ID="DropDownList2" runat="server"
AutoPostBack="false" OnChange="invis2();";>

style.visibility doesn't work with every browser style.display  does.
guru_samiCommented:
if you want to javascript way..you nedd to get the ddl element first..like:

function invis2() {
             var ddl = document.getElementById('DropDownList2');
             if (ddl.value != "Prop") {
                       document.getElementById('TextBox7').style.visibility = 'Hidden';
                 document.getElementById('Label1').style.visibility = 'Hidden';
             }
             else {
                 document.getElementById('TextBox7').style.visibility = 'Visible';
                 document.getElementById('Label1').style.visibility = 'Visible';
               }
         }
Mike MillerSoftware EngineerAuthor Commented:
I got it to work with visibility but not with display


<asp:DropDownList ID="DropDownList2" runat="server" Width="325px" OnChange="invis2();"></asp:DropDownList>
    <script language="JavaScript" type="text/javascript">
        function invis2() {
            var ddl = document.getElementById('DropDownList2');
            if (ddl.value != "Prop") {
                document.getElementById('TextBox7').style.display = 'none';
                document.getElementById('Label1').style.display = 'none';
            }
            else {
                document.getElementById('TextBox7').style.display = 'inline';
                document.getElementById('Label1').style.display = 'inline';
            }
        }
</script>

Open in new window

guru_samiCommented:
-->but not with display
are you getting any errors?
Mike MillerSoftware EngineerAuthor Commented:
No, no errors just nothing shows up.
13598Commented:
Try:
document.getElementById('TextBox7').style.display = '';
                document.getElementById('Label1').style.display = '';
13598Commented:
Try:
  <script language="JavaScript" type="text/javascript">
        function invis2() {
            var ddl = document.getElementById('DropDownList2');
            if (ddl.value != "Prop") {
                document.getElementById('TextBox7').style.display = 'none';
                document.getElementById('Label1').style.display = 'none';
            }
            else {
                document.getElementById('TextBox7').style.display = '';
                document.getElementById('Label1').style.display = '';
            }
        }
</script>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
Ok I think I may know why it was not working. I was trying to not display TextBox7 and Label1 on load by calling another function. What is the best way for these not to be visible on load?
13598Commented:
Make their default style.display be none.
Mike MillerSoftware EngineerAuthor Commented:
Ok maybe that wasn't it then because that's what I did


<body onload='invis();'>
<script language="JavaScript" type="text/javascript">
        function invis() {

                document.getElementById('TextBox7').style.display = 'none';
                document.getElementById('Label1').style.display = 'none';
            }
</script>

Open in new window

13598Commented:
Where do you define your textbox? Define it as style.display = 'none'
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.