In this example we have bind the dropdownlist using jquery ajax in asp.net. In this example I am used the generic hander to get the data from oracle database. Suppose we have table named category its columns categoryId, categoryName
CREATE TABLE Category
(
PK_CATEGORY_ID NUMBER,
CATEGORY_NAME varchar2(100),
IS_ACTIVE varchar(1)
)
I have used generic handler is given below
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Data;
public class Category : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
Ticket DataAccess = new Ticket();
int IsuuedId = int.Parse(rq);
DataTable results = DataAccess.GetCategory(IsuuedId);//get data from oracle client I have used oracle helper
// context.Response.Write("{\"eric\":\"12345\"}");
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in results.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in results.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
context.Response.Write(serializer.Serialize(rows));
results.Dispose();
}
public bool IsReusable
{
get
{
return false;
}
}
}
In aspx page we need the following stuff is given below
<script src="Scripts/jquery1.5.1.min.js" type="text/javascript"></script>
$.ajax({
url: 'ajax/Category.ashx',
type: 'POST',
cache: false,
data: jsonData,
success: function (data) {
$.each(data, function (key, value) {
$("#<%=drpCategory.ClientID %>").append($("<option></option>").val(value.CategoryId).html(value.CategoryName));
});
},
error: function (data) {
alert("Error occur in applicattion. Please contact Team");
}
});
<asp:DropDownList id="drpCategory " tabindex="7" runat="server">
</asp:DropDownList>
No comments:
Post a Comment