-
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??
Thanks
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks