Do various things by JSON in ASP.net -3
Bind dropdown list by JSON
Form Source
<div>
<table>
<tr>
<td align="right">
<asp:Label ID="Label4139"
runat="server"
ForeColor="Red"
Text="*"></asp:Label>
<asp:Label ID="Label31"
runat="server"
Text="Region
:"></asp:Label>
</td>
<td align="left">
<asp:DropDownList ID="ddl_Region"
runat="server"
Style="min-width: 100px; max-width: 250px"
ValidationGroup="OISave">
<asp:ListItem Value="0">-Select-</asp:ListItem>
<asp:ListItem Value="1">AME</asp:ListItem>
<asp:ListItem Value="2">EMEA</asp:ListItem>
<asp:ListItem Value="3">INDIA</asp:ListItem>
<asp:ListItem Value="4">SEAP</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator
ID="rfv_Region"
runat="server"
ControlToValidate="ddl_Region"
Display="Dynamic"
ErrorMessage="Please
Select Region" InitialValue="0" SetFocusOnError="True"
ValidationGroup="OISave">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label55"
runat="server"
Text="Country
:"></asp:Label>
</td>
<td align="left">
<asp:DropDownList ID="ddl_Country"
runat="server"
Style="min-width: 100px; max-width: 250px"
ValidationGroup="OISave">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
SCRIPT
json2.js – Search on google for Jsfile
<script src="../App_Themes/CGDNA/json2.js"
type="text/javascript"></script><%--important --%>
<script type="text/javascript"
language="JavaScript">
$(document).ready(function()
{
$("#<%=ddl_Region.ClientID %>").change(function() {
BindCountry();
});
});
function BindCountry() {
var items = {};
items.vRegionCode = $("#<%=ddl_Region.ClientID
%>").val();
$.ajax({
type: "POST",
url: "frmBidInformation.aspx/LoadCountryDetails",
data: '{items: ' + JSON.stringify(items) + '}',
contentType: "application/json;
charset=utf-8",
dataType:
"json",
async: false,
success: function(data) {
//alert(data.d);
$("#<%=ddl_Country.ClientID %> option").remove();
$.each(data.d, function(key, value) {
$("#<%=ddl_Country.ClientID
%>").append($("<option></option>").val
(value.CountryCode).html(value.CountryName));
});
}
});
}
</script>
Code Behind
public class Country
{
public string
CountryCode { get; set;
}
public string
CountryName { get; set;
}
public string
vRegionCode { get; set;
}
}
[WebMethod]
public static List<Country> LoadCountryDetails(Country items)
{
DataTable dt_Country = new
DataTable();
clsMaster objMaster = new
clsMaster();
objMaster.vRegionCode = items.vRegionCode;
dt_Country =
objMaster.FUN_FILL_COUNTRY_REGIONWISE();
List<Country>
lstCountry = new List<Country>();
try
{
lstCountry.Add(new Country { CountryCode = "0",
CountryName = "-Select-" });
if (dt_Country.Rows.Count > 0)
{
foreach (DataRow
dr in dt_Country.Rows)
{
lstCountry.Add(new Country()
{
CountryCode = dr["con_code"].ToString(),
CountryName = dr["con_name"].ToString()
});
}
}
}
catch (Exception
exp)
{
throw exp;
}
return lstCountry;
}
No comments:
Post a Comment