How to use button click event for database connectivity and enabling java script
in my project i am using a button click event to delete selected record in the database. so while doing this i have to call a java script in the same event to ask confirmation for deleting the record. how to provide code in .aspx.cs file to enable java script

vikas tiwari2 reputationPosted Nov 22, 2011, 11:21 PM EST
NarayanRank #8,477 · 221 reputationPosted Nov 22, 2011, 11:23 PM EST
Something like this
Button1.Attributes.Add("OnClientClick","return Confirm('Are you sure to delete?')");
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 23, 2011, 12:35 AM EST
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "return ConfirmOnDelete();");
}
}
Thanks
Ashok6 reputationPosted Nov 24, 2011, 12:58 AM EST
please help me in proceeding further
thanks a lot
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 24, 2011, 1:20 AM EST
Hi Ashok,
We don't know the actual requirement of your's.
If you are displaying the data in a grid view, which contains a checkbox and a button and if we check the selected record and then want to delete it. Then use the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Text="Label">
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection con;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindGridView();
totalrecords();
}
private void totalrecords()
{
con = new SqlConnection(connStr);
//cmd.Connection = con;
con.Open();
str = "select count(*) from employee ";
SqlCommand cmd = new SqlCommand(str, con);
int row = (int)cmd.ExecuteScalar();
Label2.Text = row.ToString();
con.Close();
}
private void BindGridView()
{
con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "Select * from employee";
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
private void DeleteRecords(StringCollection sc)
{
con = new SqlConnection(connStr);
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
const string sqlStatement = "DELETE FROM employee WHERE EmpId";
sb.AppendFormat("{0}='{1}'; ", sqlStatement, item);
}
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
protected void ButtonDelete_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb != null)
{
if (cb.Checked)
{
id = GridView1.Rows[i].Cells[1].Text;
sc.Add(id);
}
}
}
DeleteRecords(sc);
BindGridView();
totalrecords();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Button b = (Button)e.Row.FindControl("ButtonDelete");
b.Attributes.Add("onclick", "return ConfirmOnDelete();");
}
}
}
Thanks