I have tried to fill 2 text boxes when a DropdownList changed.
The text in text box 1 is Name of ID_1.
The text in text box 2 is Age of ID_1
And if I choose another choices in the dropdownlist, the text in text box 1 and text 2 will be changed.
For example,
+----+-----------+--------------+--------------+
| Dropdownlist | TextBox1 | TextBox2 |
+----------------+--------------+--------------+
| 1 | John | 23 |
+----------------+--------------+--------------+
| 2 | Vivian | 43 |
+----------------+--------------+--------------+
This is my database.
+----+-----------+--------------+
| id | Name | Age |
+----+-----------+--------------+
| 1 | John | 23 |
+----+-----------+--------------+
| 2 | Vivian | 43 |
+----+-----------+--------------+
My code from .aspx
$('#<%:DropDownA.ClientID%>').change(function () {
changeData();
});
function changeData() {
var txt1 = $('#<%:DropDownA.ClientID%>').val();
var txt2 = $('#<%:DropDownA.ClientID%>').val();
$('input[id="TextBox1"]').val(txt1);
$('input[id="TextBox2"]').val(txt2);
};
changeData();
<asp:DropDownList ID="DropDownA" runat="server" CssClass="form-control" DataSourceID="SqlDataFromDatabase" DataTextField="id" DataValueField="name"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataFromDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:asrsDBConnectionString %>" SelectCommand="SELECT * FROM [table]"></asp:SqlDataSource>
<input class="form-control" id="TextBox1" value="xxxx" type="text" style="color: green;" disabled>
<input class="form-control" id="TextBox2" value="xxxx" type="text" style="color: green;" disabled>
Thank you in advance for giving me the solution or suggestion.
The Solution.
** Please follow this link for checking how to split the data: **
How to split the string using jQuery or JavaScript?
$('#<%:DropDownA.ClientID%>').change(function () {
changeData();
});
function changeData() {
var data1 = $('#<%:DropDownA.ClientID%>').val();
var arr = data1.split(';');
$('input[id="TextBox1"]').val(arr[0]);
$('input[id="TextBox2"]').val(arr[1]);
};
changeData();
<asp:DropDownList ID="DropDownA" runat="server" CssClass="form-control" DataSourceID="SqlDataFromDatabase" DataTextField="id" DataValueField="name"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataFromDatabase" runat="server" ConnectionString="<%$ ConnectionStrings:asrsDBConnectionString %>" SelectCommand="Select (Name + ';' + Age) NameAge, id from [table]"></asp:SqlDataSource>
<input class="form-control" id="TextBox1" value="xxxx" type="text" style="color: green;" disabled>
<input class="form-control" id="TextBox2" value="xxxx" type="text" style="color: green;" disabled>