Description:-
In this example we explain that how to set Dropdown
List Selected Value (Text) based on text /value in asp.net using jQuery. Or set
Dropdown List selected value or text using jQuery. Set dropdown value by text
using jQuery. Setting select list ‘selected’ based on text using jQuery.
Setting the selected value of a select control via its text description using
jQuery.
Sometime we have requirement like when we edit record
at that time the DrodownList value will be automatically set based on the value
retrieve from the database.
JQuery provide map functions to set the Dropdown List
selected value based on text or value.
setdrpvalue.aspx:
<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="setdrpvalue.aspx.cs" Inherits="WebApplication1.setdrpvalue"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>set dropdownlist selected value based on
text/value in asp.net using jQuery
</title>
<script
type="text/javascript"
src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script
type="text/javascript">
$(function
() {
//
Set Dropdownlist Selected Value by Text
$('#btnText').click(function () {
var
selectedText = 'Australia';
$('#drpCountry
option').map(function () {
if
($(this).text() == selectedText) return this;
}).attr('selected', 'selected');
})
//
Set Dropdownlist Selected Value by Value
$('#btnValue').click(function () {
var
selectedValue = '3';
$('#drpCountry
option').map(function () {
if
($(this).val() == selectedValue) return this;
}).attr('selected', 'selected');
})
})
</script>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<select id="drpCountry">
<option value="1">India</option>
<option value="2">Pakistan</option>
<option value="3">Australia</option>
<option value="4">USA</option>
<option value="5">UK</option>
</select>
</div>
<input
type="button"
value="Set By
Text" id="btnText"
/>
<input
type="button"
value="Set By
Value" id="btnValue"
/>
</form>
</body>
</html>
0 comments:
Post a Comment