Hi,
I have gridview inside gridview have checkbox.
I want to update record in database after checked on gridview through jquery please help how can do this?
The checkbox is is html input type.
I want to update status like after click on checkbox status false then true or true then false ass well as update also in database table ?
using jquery only ?
please help?
Krishna Rajput SinghPosted Oct 15, 2015, 3:15 AM
Sabyasachi MishraPosted Oct 15, 2015, 2:28 AM
Sabyasachi MishraPosted Oct 16, 2015, 4:19 AM
Davin MartynPosted Oct 16, 2015, 3:49 AM
for (var i = 0; i < result.d.length; i++) {
var v = result.d[i].ID;
$("#tbDetails").append("" + v + " " + result.d[i].ReportName + " " + result.d[i].ReportDescription + " " + result.d[i].Status + " " + "" + " ");
}
I want to give the var v ;
value in checkbox but the value of v is not appearing please help
Sabyasachi MishraPosted Oct 15, 2015, 1:20 PM
Mukesh KumarPosted Oct 15, 2015, 1:17 PM
Davin MartynPosted Oct 15, 2015, 10:26 AM
Hello Mishra sir,
<script type="text/javascript">
$(document).ready(function () {
$("#btnShowData").click(function () {
var param = { str: $('#txtCity').val(), str1: $('#Txt').val() };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default3.aspx/BindEmployees",
data: JSON.stringify(param),
//data: "{}",
dataType: "json",
success: function (result) {
for (var i = 0; i < result.d.length; i++) {
var v = result.d[i].ID;
$("#example").append("" + v + " " + result.d[i].ReportName + " " + result.d[i].ReportDescription + " " + result.d[i].Status + " " + "checkbox" class="chcktbl1" id="chcktbl1" name="chcktbl1" id="<%= result.d[i].ID%>" /> + " ");
}
},
error: function (result) {
alert("Error");
}
});
});
});
please possible then update here ?Sabyasachi MishraPosted Oct 15, 2015, 7:55 AM
Sanjay SabariyaPosted Oct 15, 2015, 6:58 AM
public static void UpdateCustomer(int Id)
{ // break point on this line
Davin MartynPosted Oct 15, 2015, 6:45 AM
Please check my script :
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('input[type=checkbox]').click(function () {
if ($(this).is(':checked')) {
var id = $('input[type=checkbox ]').val();
$.ajax({
url: "Default3.aspx/UpdateCustomer",
data: JSON.stringify(id),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//write msg updated
alert("Record successfully updated!!!");
//location.reload();
}
});
}
return false;
});
});
script>
and my gridview given above :
Sanjay SabariyaPosted Oct 15, 2015, 6:40 AM
public static void UpdateCustomer(int Id)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE report SET status = @status, WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Davin MartynPosted Oct 15, 2015, 6:25 AM
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('input[type=checkbox]').click(function () {
if ($(this).is(':checked')) {
var id = $('input[type=checkbox ]').val();
$.ajax({
url: "Default3.aspx/UpdateCustomer",
data: JSON.stringify(id),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//write msg updated
alert("Record successfully updated!!!");
//location.reload();
}
});
}
return false;
});
});
script>
AND METHOD :
public static void UpdateCustomer(int Id)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE report SET status = @status, WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Please check this is right if any problem then please update accordinglySanjay SabariyaPosted Oct 15, 2015, 6:17 AM
Davin MartynPosted Oct 15, 2015, 6:00 AM
Davin MartynPosted Oct 15, 2015, 5:59 AM
Sanjay SabariyaPosted Oct 15, 2015, 5:29 AM
Sabyasachi MishraPosted Oct 15, 2015, 4:56 AM
Davin MartynPosted Oct 15, 2015, 4:18 AM
This is my script :
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('input[type=checkbox]').click(function () {
if ($(this).is(':checked')) {
var id = $('input[type=checkbox ]').val();
$.ajax({
url: "Default3.aspx/UpdateCustomer",
data: JSON.stringify(id),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
//write msg updated
alert("Record successfully updated!!!");
//location.reload();
}
});
}
return false;
});
});
script>
public static void UpdateCustomer(int Id, int status)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE report SET status = @status, WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@status", status);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
I am using sqlserver asp.net, and jquery$(document).ready(function () {
$("#btnShowData").click(function () {
var param = { str: $('#txtCity').val(), str1: $('#Txt').val() };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default3.aspx/BindEmployees",
data: JSON.stringify(param),
//data: "{}",
dataType: "json",
success: function (result) {
for (var i = 0; i < result.d.length; i++) {
var v = result.d[i].ID;
$("#gvData").append("" + v + " " + result.d[i].ReportName + " " + result.d[i].ReportDescription + " " + result.d[i].Status + " " + "" + " ");
}
},
error: function (result) {
alert("Error");
}
});
});
});