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
425 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
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:mwmiller78
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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…

919 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

13 Experts available now in Live!

Get 1:1 Help Now