Pages

Wednesday, 24 April 2013

How to Put confirmation box with yes/no button options using modal popup in c# and VB Asp.net

How to Put  confirmation box with yes/no button options using modal popup in c# Asp.net

Program:

.Aspx File

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<style type="text/css">
.modalBackground
{
background-color: Gray;
z-index: 10000;
}

.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
Table.Gridview{border:solid 1px #df5015;}
.Gridview th{color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;padding:0.5em 0.5em 0.5em 0.5em;text-align:center}
.Gridview td{border-bottom-color:#f0f2da;border-right-color:#f0f2da;padding:0.5em 0.5em 0.5em 0.5em;}
.Gridview tr{color: Black; background-color: White; text-align:left}
:link,:visited { color: #DF4F13; text-decoration:none }

</style>
<script language="javascript" type="text/javascript">
    function closepopup() {
        $find('ModalPopupExtender1').hide();
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="ScriptManager1" runat="server">
</ajax:ToolkitScriptManager>
<asp:UpdatePanel ID="updatepnl1" runat="server">
<ContentTemplate>
<asp:GridView runat="server" ID="gvDetails" CssClass="Gridview"
DataKeyNames="ID" AutoGenerateColumns="false">
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Designation" HeaderText="Designation" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" ImageUrl="~/Images/Delete.png" runat="server" onclick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblresult" runat="server"/>
<asp:Button ID="btnShowPopup" runat="server" style="display:none" />
<ajax:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlpopup"
CancelControlID="btnNo" BackgroundCssClass="modalBackground">
</ajax:ModalPopupExtender>
<asp:Panel ID="pnlpopup" runat="server" BackColor="White" Height="100px" Width="400px" style="display:none">
<table width="100%" style="border:Solid 2px #D46900; width:100%; height:100%" cellpadding="0" cellspacing="0">
<tr style="background-image:url(Images/header.gif)">
<td style=" height:10%; color:White; font-weight:bold;padding:3px; font-size:larger; font-family:Calibri" align="Left">Confirm Box</td>
<td style=" color:White; font-weight:bold;padding:3px; font-size:larger" align="Right">
<a href = "javascript:void(0)" onclick = "closepopup()"><img src="Images/Close.gif" style="border :0px" align="right"/></a>
  </td>
</tr>
<tr>
<td colspan="2" align="left" style="padding:5px; font-family:Calibri">
<asp:Label ID="lblUser" runat="server"/>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td>
</td>
<td align="right" style="padding-right:15px">
<asp:ImageButton ID="btnYes" OnClick="btnYes_Click" runat="server" ImageUrl="~/Images/btnyes.jpg"/>
<asp:ImageButton ID="btnNo" runat="server" ImageUrl="~/Images/btnNo.jpg" />
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

.Aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SURANI-PC\\SA;Initial Catalog=demo;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindUserDetails();
        }
    }
    protected void BindUserDetails()
    {
        //connection open
        con.Open();
        //sql command to execute query from database
        SqlCommand cmd = new SqlCommand("Select * from Table_1", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        //Binding data to gridview
        gvDetails.DataSource = ds;
        gvDetails.DataBind();
        con.Close();
    }
  protected void btnYes_Click(object sender, ImageClickEventArgs e)
  {
      //getting ID of particular row
      int ID = Convert.ToInt32(Session["ID"]);
      con.Open();
      SqlCommand cmd = new SqlCommand("delete from Table_1 where ID=" + ID, con);
      int result = cmd.ExecuteNonQuery();
      con.Close();
      if (result == 1)
      {
      lblresult.Text = Session["Name"] + " Details deleted successfully";
      lblresult.ForeColor = Color.Green;
      BindUserDetails();
      }
  }
  protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btndetails = sender as ImageButton;
        GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
        Session["ID"] = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
        Session["Name"] = gvrow.Cells[0].Text;
        lblUser.Text = "Are you sure you want to delete " + Session["Name"] + " Details?";
        ModalPopupExtender1.Show();
    }
}

VB Code:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Class VBCode
    Inherits System.Web.UI.Page
    Private con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Not IsPostBack Then
            BindUserDetails()
        End If
    End Sub
    Protected Sub BindUserDetails()
        'connection open
        con.Open()
        'sql command to execute query from database
        Dim cmd As New SqlCommand("Select * from UserDetails", con)
        cmd.ExecuteNonQuery()
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        'Binding data to gridview
        gvDetails.DataSource = ds
        gvDetails.DataBind()
        con.Close()
    End Sub
    Protected Sub btnYes_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        'getting userid of particular row
        Dim userid As Integer = Convert.ToInt32(Session("UserId"))
        con.Open()
        Dim cmd As New SqlCommand("delete from UserDetails where UserId=" & userid, con)
        Dim result As Integer = cmd.ExecuteNonQuery()
        con.Close()
        If result = 1 Then
            lblresult.Text = Convert.ToString(Session("UserName")) & " Details deleted successfully"
            lblresult.ForeColor = Color.Green
            BindUserDetails()
        End If
    End Sub
    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        Dim btndetails As ImageButton = TryCast(sender, ImageButton)
        Dim gvrow As GridViewRow = DirectCast(btndetails.NamingContainer, GridViewRow)
        Session("UserId") = gvDetails.DataKeys(gvrow.RowIndex).Value.ToString()
        Session("UserName") = gvrow.Cells(0).Text
        lblUser.Text = "Are you sure you want to delete " & Convert.ToString(Session("UserName")) & " Details?"
        ModalPopupExtender1.Show()
    End Sub
End Class

Demo:


How to Put  confirmation box with yes/no button options using modal popup in c# and VB Asp.net
How to Put  confirmation box with yes/no button options using modal popup in c# and VB Asp.net

No comments:

Post a Comment