use javascript to make a textbox visible on DDL selection

I'm using Visual studio 2013. I have a regulare .aspx web forms page.

So on my page i have a asp DropDownList and a ASP Textbox.

This is my code:

TestPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DDL_ASP_EnableDisableTextBox1.aspx.cs" Inherits="Wizards2.DDL_ASP_EnableDisableTextBox1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Make TextBox Visible on DDL Selection</title>

    <script type="text/javascript">

        // disable textbox by default
        //Textbox1.disabled = true;
        document.getElementById('TextBox1').style.visibility = 'hidden';

        function Enable() {

            document.getElementById('TextBox1').style.visibility = 'visible';
            //document.getElementById("TextBox1").disabled = false;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDown_Fruit" runat="server" AutoPostBack="True" onchange="Enable();">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Banana</asp:ListItem>
            <asp:ListItem>Pear</asp:ListItem>
            <asp:ListItem>Watermelon</asp:ListItem>
        </asp:DropDownList>
        <br /><br /><br /><br />                  
        <asp:TextBox runat="server" ID="TextBox1" />       

    </div>
    </form>
</body>
</html>

Open in new window


This is what I want:

When the page launches I want TextBox1 to be hidden.
Then if I select any item from the Drop Down List then I want the textbox to become visible.


Where am I making a mistake in my code above?
LVL 1
maqskywalkerAsked:
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.

Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
since your DDL is a server control, when the page is loaded, it's ID is not Textbox1. you can verify this via doing a view source.

what you want to do is something like:

    <script type="text/javascript">

        // disable textbox by default
        //Textbox1.disabled = true;
        document.getElementById('<%=TextBox1.ClientID %>').style.visibility = 'hidden';

        function Enable() {

            document.getElementById('<%=TextBox1.ClientID %>').style.visibility = 'visible';
            //document.getElementById("<%=TextBox1.ClientID %>").disabled = false;

        }
    </script>

Open in new window

you should also move the script down towards the end of the doc, right before the closing </body> tag, to make sure the DOM has fully loaded
0
maqskywalkerAuthor Commented:
Per your suggestion I revised my code. This is my code now... Almost there.

TestPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="Wizards2.TestPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Make TextBox Visible on DDL selection</title>

    <script type="text/javascript">

        function MakeHidden() {
            // hide textbox
            // asp textbox
            document.getElementById('<%=TextBox1.ClientID %>').style.visibility = 'hidden';
            // html textbox
            //document.getElementById('TextBox1').style.visibility = 'hidden';
        }

        function MakeVisible() {
            // make textbox visible
            // asp textbox
            document.getElementById('<%=TextBox1.ClientID %>').style.visibility = 'visible';
            // html textbox
            //document.getElementById('TextBox1').style.visibility = 'visible';
        }
         
    </script>

</head>

<!--calls this function on page load-->
<body onload="MakeHidden()">

    <form id="form1" runat="server">

    <div>
        <asp:DropDownList ID="DropDown_Fruit" runat="server" AutoPostBack="True" onchange="MakeVisible()">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Banana</asp:ListItem>
            <asp:ListItem>Pear</asp:ListItem>
            <asp:ListItem>Watermelon</asp:ListItem>
        </asp:DropDownList>
        <br /><br /><br /><br />                  
        <asp:TextBox runat="server" ID="TextBox1" />       
    </div>

    </form>
</body>
</html>

Open in new window


When I run this page now, the textbox is hidden on load which is exactly what I want.
But when I select any item from the dropdownlist the textbox is visible for like less than 1 second and it disappears.

This is a video of me running my page. You can see what I'm talking about.
http://youtu.be/Wbq1b6bBBd4

Any ideas of why the textbox appears for like half a second and then disappears? It should not disappear. After I make a selection it should appear and stay there on the screen.
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
it's going back to a hidden state because you have AutoPostBack="True" on your DDL. Remove it (or set it to false), assuming you don't want to submit the form during the OnChange event
0

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.