Aspx Code:-
first of all you add the j query library file. the you install the Json . How to add
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="Jquerydropdownlist.Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var IDD =0;
$(document).ready(function () {
Country_bind();
Data_bind();
});
function Country_bind()
{
$.ajax({
url: 'Registration.aspx/Get_Country',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{}",
async: false,
success: function (ds) {
ds = JSON.parse(ds.d);
$("#tbl").find("td:gt(0)").remove();
for (var i = 0; i < ds.length; i++)
{
$("#ddlcountry").append($('<option/>').attr("value", ds[i].cid).text(ds[i].cname));
}
},
error: function () {
alert('Get Country Error!!');
}
});
}
function Savedata() {
$.ajax({
url: 'Registration.aspx/Insertdata',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{A:'" + $("#Textname").val() + "',B:'" + $("#ddlcountry").val() + "',C:'" + $("#Textage").val() + "',D:'" + $("#Textaddress").val() + "',E:'" + $('input:radio[name=A]:checked').val() + "',F:'"+IDD+"'}",
async: false,
success: function () {
alert('Insert Successfull !!')
},
error: function () {
alert('Insert Error!!');
}
});
}
function Data_bind() {
$.ajax({
url: 'Registration.aspx/Getdata',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{}",
async: false,
success: function (ds) {
ds = JSON.parse(ds.d);
$("#tbl").find("tr:gt(0)").remove();
for (var i = 0; i < ds.length; i++)
{
$("#tbl").append('<tr> <td>' + ds[i].Name + '</td> <td>' + ds[i].cname + '</td> <td>' + (ds[i].Gender == "1" ? "Male" : ds[i].Gender == "2" ? "Female" : "Others") + '</td> <td>' + ds[i].Age + '</td> <td>' + ds[i].Address + '</td> <td><input type="button" id="btnedit" value="Edit" onclick="Editdata(' + ds[i].Empid + ')" /></td> <td><input type="button" id="btndelete" value="Delete" onclick="Delete_data(' + ds[i].Empid + ')" /></td> </tr>');
}
},
error: function () {
alert('databind error !!');
}
});
}
function Delete_data(Empid) {
$.ajax({
url: 'Registration.aspx/Delete',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{A:'"+Empid+"'}",
success: function () {
alert('Delete successfull !!')
Data_bind();
},
error: function () {
alert('Delete Error !!');
}
});
}
function Editdata(Empid) {
$.ajax({
url: 'Registration.aspx/Edit_data',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{A: '"+Empid+"'}",
success: function (ds) {
ds = JSON.parse(ds.d);
$("#Textname").val(ds[0].Name);
$("#ddlcountry").val(ds[0].Country);
$("#Textage").val(ds[0].Age);
$("#Textaddress").val(ds[0].Address);
$("#btnsave").val("Update");
IDD=Empid;
},
error: function () {
alert('Edit error !!');
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="Textname" /></td>
</tr>
<tr>
<td>Country:</td>
<td>
<select id="ddlcountry">
<option value="0">--select--</option>
</select>
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" value="1" name="A" />Male
<input type="radio" value="2" name="A" />Female
<input type="radio" value="3" name="A" />Other
</td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" id="Textage" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" id="Textaddress" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" id="btnsave" value="Save" onclick="Savedata()" /></td>
</tr>
</table>
<table id="tbl" border="1" style="background-color:pink">
<tr style="background-color:maroon;color:white">
<th>Name</th>
<th>Country</th>
<th>Gender</th>
<th>Age</th>
<th>Address</th>
<th></th>
</tr>
</table>
</div>
</form>
</body>
</html>
.CS page:- coding
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.Configuration;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Jquerydropdownlist
{
public partial class Registration : System.Web.UI.Page
{
static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string Get_Country()
{
string dt = "";
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
dt = JsonConvert.SerializeObject(ds.Tables[0]);
}
return dt;
}
[WebMethod]
public static void Insertdata(string A,int B,int C,string D,int E,int F)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_insert_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", F);
cmd.Parameters.AddWithValue("@Name", A);
cmd.Parameters.AddWithValue("@Country", B);
cmd.Parameters.AddWithValue("@Age", C);
cmd.Parameters.AddWithValue("@Address", D);
cmd.Parameters.AddWithValue("@Gender", E);
cmd.ExecuteNonQuery();
con.Close();
}
[WebMethod]
public static string Getdata()
{
string dt = "";
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if(ds.Tables[0].Rows.Count>0)
{
dt=JsonConvert.SerializeObject(ds.Tables[0]);
}
return dt;
}
[WebMethod]
public static string Edit_data(int A)
{
string dt = "";
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_edit", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", A);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
dt = JsonConvert.SerializeObject(ds.Tables[0]);
}
return dt;
}
[WebMethod]
public static void Delete(int A)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_delete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", A);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
No comments:
Post a Comment