TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jebin Dhanush
NA
40
6k
Bind Dropdown using ajax
Nov 26 2018 4:00 AM
hi ,
dropdown doesnot bind
my code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ajax.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
debugger;
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetCustomers",
data: '{}',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (r) {
$("DropDownList1").empty();
$("DropDownList1").append("<option value='0'>--Select--</option>");
$.each(r.d, function (key, value) {
$("DropDownList1").append("<option></option>").val(value.Value), html(value.Text);
});
},
error: function (xhr, status, text) {
console.log(xhr.status);
console.log(xhr.text);
console.log(xhr.responseText);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:300px">
<legend>
<asp:DropDownList ID="DropDownList1" runat="server" width="130"></asp:DropDownList></legend>
</fieldset>
</div>
</form>
</body>
</html>
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Configuration;
namespace ajax
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<ListItem> GetCustomers()
{
string query = "SELECT CustomerId, Name FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["jk"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
List<ListItem> customers = new List<ListItem>();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new ListItem
{
Value = sdr["CustomerId"].ToString(),
Text = sdr["Name"].ToString()
});
}
}
con.Close();
return customers;
}
}
}
}
}
code doesnot works
Reply
Answers (
2
)
How to Bind Dropdown using ajax
What all are the report tools in asp.net ?