confirm delete in gride view
//file1.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TxtJobID" runat="server"></asp:TextBox>
<asp:TextBox ID="TxtJobTitle" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Update" OnClientClick="return confirm('Are You Sure');" Font-Bold="True" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server" Text="Label" Width="474px"
style="background-color: #00cccc"></asp:Label><br />
<br />
<asp:DataGrid id="DataGrid1" OnItemCommand="ItemCommand"
style="Z-INDEX: 101; LEFT: 175px; TOP: 105px"
runat="server" CssClass="tx-tbl-Gr-lml" Width="474px" AllowPaging="true" >
<Columns>
<asp:ButtonColumn Text="Edit" ButtonType="PushButton" CommandName="Edit">
</asp:ButtonColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete">
</asp:ButtonColumn>
</Columns>
</asp:DataGrid><br />
</div>
</form>
</body>
//file1.aspx.cs
public partial class _Default : System.Web.UI.Page
{
OracleConnection cn;
OracleCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
cn = new OracleConnection("data source=dev;user id=hr;password=hr");
if (!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
string strsql = "select job_id,job_title from jobs";
OracleDataAdapter da = new OracleDataAdapter(strsql, cn);
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource = dt;
DataGrid1.DataBind();
}
protected void ItemCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
FillTheData(e.Item.Cells[2].Text, e.Item.Cells[3].Text);
lblMessage.Text = "";
}
if (e.CommandName == "Delete")
{
lblMessage.Text = "Need Code to call confirm() frunction ";
}
}
void FillTheData(string job_id, string job_title)
{
TxtJobID.Text = job_id;
TxtJobTitle.Text = job_title;
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string strsql = "Update jobs set job_title="+"'"+TxtJobTitle.Text+"'"+" where job_id="+"'"+TxtJobID.Text+"'";
cmd = new OracleCommand(strsql, cn);
cn.Open();
cmd.ExecuteNonQuery();
BindData();
lblMessage.Text = "Updated Successfully";
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
finally
{
cn.Close();
}
}
}
Re: confirm delete in gride view
You can use javascript for confirmation. Just convert your delete button to a template fieldin the grid. Replace your data grid code with
Code:
<asp:DataGrid id="DataGrid1" OnItemCommand="ItemCommand"
style="Z-INDEX: 101; LEFT: 175px; TOP: 105px"
runat="server" CssClass="tx-tbl-Gr-lml" Width="474px" AllowPaging="true" >
<Columns>
<asp:ButtonColumn Text="Edit" ButtonType="PushButton" CommandName="Edit">
</asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="return confirmdelete();" CausesValidation="false" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
and put this javascript function in the head element of your page after the title.
Code:
<script language="javascript" type="text/javascript">
function confirmdelete()
{
var ok = confirm("Should I delete");
return ok;
}
</script>
Now if you click on the delete button you will be asked for confirmation. And the page will post back only if you click on the ok button.
Warm Regards,
Re: confirm delete in gride view
Thanks alot the code you provided worked for me.
Once again thanks