Solved

Populating a textbox with a database value corresponding to a selection made in a dropdown menu ASP.NET

Posted on 2015-01-27
2
463 Views
Last Modified: 2015-04-11
I have a dropdown list with values tied to a database.
I have a textbox that i need to populate with a value based on the selection made in the dropdown.

The table that has all of the selections in the dropdown is 2 columns. 1 is what is in the dropdown, the other has the corresponding value that needs to appear in the textbox when a selection is made.

What is the best way to do this without posting back?
0
Comment
Question by:Mike Miller
2 Comments
 
LVL 8

Accepted Solution

by:
johny_bravo1 earned 500 total points
ID: 40574524
Add this scripts in your <head> section

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

<script type="text/javascript">
    $(document).ready(function () {
        //We are binding onchange event using jQuery built-in change event
        $('#ddlList1').change(function () {
            //get selected value and check if subject is selected else show alert box
            var SelectedValue = $("#ddlList1").val();
            if (SelectedValue > 0) {
                //get selected text and set to label
                var SelectedText = $("#ddlList1 option:selected").text();
                lblSelectedText.innerHTML = SelectedText;
                //set selected value to textbox
            $('#txtFirstName').val(SelectedValue);
            
            //if you want to show the text then uncomment below line      
            //$('#txtFirstName').val(SelectedText);
               
            } else {
                //reset textbox values
            $('#txtFirstName').val('');
               
            }
        });
    });
</script>

and make your dropdown ClientIDMode='Static'

 <asp:DropDownList ID="ddlList1" runat="server" ClientIDMode="Static">
0
 

Author Comment

by:Mike Miller
ID: 40575374
I probably should have mentioned this earlier but both controls are in a repeater called Repeater2 which is inside a GridView called GridView1. How would I make sure the correct controls are submitted to the script?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question