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
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 26
Json and ajax 1 22
Record locking on classic ASP 3 40
Add - in a loop to first and last element. 6 42
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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

726 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