DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2004

    Javascript for dropdown listbox in asp.net project

    I have a dropdown list populated from the database with the last options being 'Others'. Now when others is selected I would like to either make visible or enable a textbox control from the client side itself instead of making a round trip to server by setting 'autopostback' to true. Is this possible and if so what is the code??

  2. #2
    Join Date
    Aug 2004
    You will have to use client script to achieve the desired result. And you need to add an event to that dropdown list while populating it. If it is not yet clear - let me know. I will make a small example.

  3. #3
    Join Date
    Aug 2004
    Assume you have two web controls: a drop down list, DropDownList1, and a textbox, Textbox1. If you know that the 'Others' option will always be last, you can add the following to your server side page load method:

    DropDownList1.Attributes.Add("onchange", "if (this.selectedIndex == this.options.length - 1) document.getElementById('" & TextBox1.ClientID & "').disabled = false; else document.getElementById('" & TextBox1.ClientID & "').disabled = true;")

    TextBox1.Enabled = False

    This server side code will add client side code that traps the onChange event of the drop down list. When the user selects an option, the client side code will see if the user selected the last option in the list ('this.selectedIndex' gives the index of the selected item and 'this.options.length - 1' gives the index of the last item in the list). If the user has selected the last item, it will enable the textbox, otherwise it will disable the textbox. TextBox1.ClientID is necessary to get the textbox's id because ASP.NET will sometimes change the id of server controls. The ClientID property gives the actual id that the control will bear in the server code.

    The "TextBox1.Enabled = False" is just there to default the textbox to disabled. You can also do this through the designer if you have Visual Studio.NET.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center