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
412 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:mwmiller78
2 Comments
 
LVL 8

Accepted Solution

by:
johny_bravo1 earned 500 total points
Comment Utility
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:mwmiller78
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now