<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_2905json_passing.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Bind Dropdownlist json</title>
<script src="../Script/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var Name=ddlObj.value;
debugger;
$.ajax({
url: "WebForm1.aspx/GetData",
data: JSON.strngify({Names:Name}),
dataType: "json",
Type:'Post',
contentType: "application/json; charset=utf-8",
success: function chandra(data)
{
debugger:
var jsdata = JSON.parse(data);
BindDropdownlist(jsdata);
var textval=jsdata.selectedIndex();
textval=document.getElementById('TextBox1');
},
error: function (data) {
alert("error found");
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="160px" onchange="chandra(this)">
</asp:dropdownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
aspx.cs code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Services;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
namespace _2905json_passing
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var data = BindDropdownlist();
DropDownList1.DataSource = data;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "Select name");
}
public static string[] BindDropdownlist()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select Name from Customers", con);
da.Fill(dt);
return dt.AsEnumerable().Select(k => k[0] + "").ToArray();
}
public static string Getdata(string Names)
{
if(BindDropdownlist().Contains(Names))
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlCommand cmd =new SqlCommand("select * from Customers" + Names ,con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
string jsdata=JsonConvert.SerializeObject(dt);
return jsdata;
}
else
return "Names are not showing";
}
}
}
in that above based on the dropdown selection that value move to the text box