Populate radiobuttonlist using jQuery
aspx code:-
<%@ 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">
$(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() + "'}",
/// How to Get The Value of Selected Radio Button Using jQuery//
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> </tr>');
}
},
error: function () {
alert('databind 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 //Group name by A//
<input type="radio" value="2" name="A" />Female
<input type="radio" value="3" name="A" />Other
</td> ///radio button checked property
</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)
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_employee_insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandType = CommandType.StoredProcedure;
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;
}
}
}
No comments:
Post a Comment