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
446 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

822 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