Registration.Aspx code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="Jquery_test_22.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 ID = 0;
$(document).ready(function () {
Country_bind();
databind();
});
function Country_bind() {
$.ajax({
url: 'Registration.aspx/country_get',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
async: false,
data: "{}",
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('Country bind error !!');
},
});
}
function Savedata() {
$.ajax({
url: 'Registration.aspx/Save_data',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{A:'" + $("#textname").val() + "', B:'" + $('input:radio[name=A]:checked').val() + "', C:'" + $("#ddlcountry").val() + "',D:'" + ID + "'}",
async: false,
success: function () {
if ($("#btnsave").val() == "Save") {
alert('insert successfull !!');
}
else if ($("#btnsave").val() == "Update")
{
alert('Update successfull !!');
}
window.location.reload(true);
},
error: function () {
if ($("#btnsave").val() == "Save") {
alert('insert error !!');
}
else if ($("#btnsave").val() == "Update") {
alert('Update error !!');
}
}
});
}
function databind()
{
$.ajax({
url: 'Registration.aspx/Get_data',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{}",
success: function (ds) {
ds = JSON.parse(ds.d);
$("#tbl").find("td:gt(0)").remove();
for (var i = 0; i < ds.length; i++)
{
$("#tbl").append('<tr><td>' + ds[i].Name + '</td> <td>' + (ds[i].Gender == "1" ? "Male" : ds[i].Gender == "2" ? "Female" : "Other") + '</td> <td>' + ds[i].Cname + '</td> <td><input type="button" id="btndelete" value="Delete" onclick="data_delete(' + ds[i].Empid + ')" /></td> <td><input type="button" id="btnedit" value="Edit" onclick="Data_edit('+ds[i].Empid+')" /></td></tr>');
}
},
error: function () {
alert('Data bind Error !!');
},
});
}
function data_delete(Empid)
{
$.ajax({
url: 'Registration.aspx/Delete_data',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{A:'" + Empid + "'}",
success: function () {
alert('Delete successfull !!');
window.location.reload(true);
},
error: function () {
alert('Delete error !!');
},
});
}
function Data_edit(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);
$("#btnsave").val("Update");
ID = Empid;
},
error: function () {
alert('Edit data 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>Gender:</td>
<td><input type="radio" name="A" value="1" />Male
<input type="radio" name="A" value="2" />Female
<input type="radio" name="A" value="3" />Other
</td>
</tr>
<tr>
<td>Country:</td>
<td><select id="ddlcountry">
<option value="0">--select--</option>
</select></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>Gender</th>
<th>Country</th>
<th>Delete</th>
<th>Edit</th>
<th></th>
</tr>
</table>
</div>
</form>
</body>
</html>
How to install json. Click here
.CS page code C#
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 Jquery_test_22
{
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 country_get()
{
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 Save_data(string A, int B, int C,int D)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_ins_upd", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Empid", D);
cmd.Parameters.AddWithValue("@Name", A);
cmd.Parameters.AddWithValue("@Gender", B);
cmd.Parameters.AddWithValue("@Country", C);
cmd.ExecuteNonQuery();
con.Close();
}
[WebMethod]
public static string Get_data()
{
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 void Delete_data(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();
}
[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;
}
}
}
No comments:
Post a Comment