Disabling a textbox when i pick an item in my dropdownlist

Last post 08-07-2007 11:42 PM by d4dennis@inspir3. 4 replies.

Sort Posts:

  • Disabling a textbox when i pick an item in my dropdownlist

    08-07-2007, 9:39 PM
    • Loading...
    • Ez416
    • Joined on 03-31-2006, 7:19 AM
    • Posts 461

    for example my items inside the dropdownlist is apple, banana, grapes

    if i pick apple my textbox1 will be disabled

    or i pick banana my textbox 2 will be disabled

    so on so forth

    In javascript please. thanks!

    ez
  • Re: Disabling a textbox when i pick an item in my dropdownlist

    08-07-2007, 10:10 PM
    Answer
    • Loading...
    • codeasp
    • Joined on 06-16-2004, 9:19 PM
    • Posts 2,153

    Does this help?

    dropdown.Attributes.Add("onChange", "javascript:return Check('" & dropdown.ClientID.ToString() & "')")
      
    <script type="text/javascript">
     function TimeCheck(ctlname)
     {
         // Get the relevant control
         var ddl = document.getElementById(ctlname);
         var txt = ddl.options[ddl.selectedIndex].text
         switch (txt)
         {
             // Do your switch statements and disable the
            // relative textbox
         }
     }
     
  • Re: Disabling a textbox when i pick an item in my dropdownlist

    08-07-2007, 10:11 PM
    Answer

    Hi there,

    Base on your requirement, i've written following block of code. Hope it helps!

    <script language="javascript" type="text/javascript">

    function OnSelectedChanged(sender,e){

    // get object reference

    var txt1 = document.getElementById('<%=TextBox1.ClientID%>');

    var txt2 = document.getElementById('<%=TextBox2.ClientID%>');

    var txt3 = document.getElementById('<%=TextBox3.ClientID%>');

    // reset object enable status

    txt1.disabled = false;

    txt2.disabled = false;

    txt3.disabled = false;

    // set status on selected type

    if ( sender.value == "Apples" )

    txt1.disabled = true;

    else if ( sender.value == "Bananas" )

    txt2.disabled = true;

    else if ( sender.value == "Grapes" )

    txt3.disabled = true;

    }

    </script>

    <body>

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

    <div>

    <asp:DropDownList ID="DropDownList1" runat="server" onchange="OnSelectedChanged(this,event);">

    <asp:ListItem Text="Apples" Value="Apples" Selected="true"></asp:ListItem>

    <asp:ListItem Text="Bananas" Value="Bananas"></asp:ListItem>

    <asp:ListItem Text="Grapes" Value="Grapes"></asp:ListItem>

    </asp:DropDownList>

    <br />

    <asp:TextBox ID="TextBox1" runat="server" Text="Apples" Enabled="false"></asp:TextBox><br />

    <asp:TextBox ID="TextBox2" runat="server" Text="Bananas"></asp:TextBox><br />

    <asp:TextBox ID="TextBox3" runat="server" Text="Grapes"></asp:TextBox><br />

    </div>

    </form>

    </body>

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: Disabling a textbox when i pick an item in my dropdownlist

    08-07-2007, 10:18 PM
    • Loading...
    • Ez416
    • Joined on 03-31-2006, 7:19 AM
    • Posts 461

    where is the line/ event where the control will be disabled?

    ez
  • Re: Disabling a textbox when i pick an item in my dropdownlist

    08-07-2007, 11:42 PM
    Answer

    Hi There,

    Ez416:

    where is the line/ event where the control will be disabled?

    If this asking me? If yes .

    if ( sender.value == "Apples" )

    txt1.disabled = true;

    else if ( sender.value == "Bananas" )

    txt2.disabled = true;

    else if ( sender.value == "Grapes" ) txt3.disabled = true;

    }

    *** txt1 or txt2 or txt3 is an object reference. to use this you have to get the object using document.getElementById(..

    Hope it helps!

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter