I want to take a user input before deleting record if the input match with the record name then record will be deleted successfully.
This is JS Function.
var object = { status: false, ele: null };
function ConfirmDelete(ev) {
if (object.status) { return true; };
swal({
title: "Are you sure?",
text: "You will not be able to recover this Record!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: true
},
function () {
object.status = true;
object.ele = ev;
object.ele.click();
});
return false;
};
This is the C# code. Code is working fine but I want to take user input before user delete the record.
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id;
id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
SqlConnection con = new SqlConnection(@".....");
con.Open();
SqlCommand cmd = new SqlCommand("Delete From [CTL].[EntityMaster] where EntityNumber = " + id);
cmd.Connection = con;
int row = cmd.ExecuteNonQuery();
con.Close();
if (row > 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "K", "swal('Deleted!','Record Deleted with Entity number =" + id + "','success')", true);
SqlConnection con1 = new SqlConnection(@"....");
con1.Open();
SqlCommand cmd1 = new SqlCommand("Select * From [CTL].[EntityMaster]");
cmd1.Connection = con1;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd1.Connection = con;
sda.SelectCommand = cmd1;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}
}
Button6.Visible = false;
}
Ok, it looks like some of those sweet alert options are older, or outdated.
So, our grid:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover" width="40%">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="cmdDelete" runat="server" Text="Delete" CssClass="btn"
OnClick="cmdDelete_Click"
OnClientClick="return ConfirmDelete(this);" >
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Our code to load is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
GridView1.DataSource =
General.MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
GridView1.DataBind();
}
We now have this:
Note how we using a plain simple button for the GV.
So the delete button code is this:
protected void cmdDelete_Click(object sender, EventArgs e)
{
Button cmdDel = (Button)sender;
GridViewRow gRow = (GridViewRow)cmdDel.NamingContainer;
int PKID = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
SqlCommand cmdSQL =
new SqlCommand("DELETE FROM tblHotels WHERE ID = @ID");
cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = PKID;
General.MyRstE(cmdSQL);
LoadGrid();
}
Ok, so now we only need our script:
This works:
<script>
var ConfirmDeleteOk = false
function ConfirmDelete(btn) {
if (ConfirmDeleteOk) {
ConfirmDeleteOk = false; // not really required
return true;
}
swal({
title: "Are you sure?",
text: "You will not be able to recover this Record!",
type: "warning",
buttons: ["Cancel", "Yes, delete it!"],
confirmButtonClass: "btn-danger",
closeOnConfirm: true
})
.then((mysweetVal) => {
if (mysweetVal) {
ConfirmDeleteOk = true;
$(btn).click();
}
})
return false;
};
</script>
And of course, we NOT going to re-type simple code like a query command and connection stuff over and over, right?
So, I have a global simple static class with the two helper routines used in above. They are:
public static DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
and the execute one "E" for those statements:
public static void MyRstE(SqlCommand cmdSQL)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (cmdSQL)
{
cmdSQL.Connection = conn;
conn.Open();
cmdSQL.ExecuteNonQuery();
}
}
}
And often I need parameters for the sql query, so I have this one:
public static DataTable MyRstP(SqlCommand cmdSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (cmdSQL)
{
cmdSQL.Connection = conn;
conn.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.