Execute code based on Server Side Yes No Confirmation Box on Button Click using C# and VB.Net in ASP.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

i want to select categories of item, then inset quantity to be updted then ones button update is clikced a message will popup asking the user if he wants to ok update, ones the user clicks ok the record is succefully updated. 

    protected void btndelet_Click(object sender, EventArgs e)
    {
        float quantityava = float.Parse(lblQuantityAvailable.Text.Trim());
        float quantity = !string.IsNullOrEmpty(txtRowQuantity.Text.Trim()) ? float.Parse(txtRowQuantity.Text.Trim()) : float.Parse("0");
        string product = ddlproductcategory.SelectedItem.Text.Trim();
         
        int inserted = 0;
        {
            float sumqty = quantityava + quantity;
            float balanceqty = sumqty;
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = str;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    cmd.CommandText = " Delete FROM Product WHERE ProductCategory  = @ProductCategory"; 
                    cmd.Parameters.AddWithValue("@ProductCategory", product); 
                    con.Open(); 
                    inserted = cmd.ExecuteNonQuery();
                    con.Close();
                    string message = string.Empty;
                    {
                        lblMessage2.Visible = true;
                        lblMessage2.Text = "Data Deleted Successfully";
                    }
                    string confirmValue = Request.Form["confirm_value"];
                    if (confirmValue == "Yes")
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
                    }
                } 
            }
        } 
        BindStockGrid();
    }

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" 
</asp:GridView>
<div class="col-lg-6">
    <p>
        <asp:DropDownList ID="ddlproductcategory" runat="server" 
        CssClass="btn btn-default dropdown-toggle" Width="100%" Font-Bold="True">
            <asp:ListItem> --Select Product--</asp:ListItem>
        </asp:DropDownList>  
    </p>
    <p class="">
        <asp:TextBox ID="txQuantity" runat="server" CssClass="form-control" Width="100%"  placeholder=" Carton Quantity"/>
    </p>
    <p class=" pull-right">
        <asp:LinkButton ID="btnsubmit" runat="server" CssClass="btn btn-info btn-circle" Height="50px" Width="150px" Font-Bold="True" OnClick="btnsubmit_Click">Update Record</asp:LinkButton>
    </p>
</div>

 

You are viewing reply posted by: pandeyism 3 months ago.
Posted 3 months ago

Hey micah,

Please refer below sample.

HTML

<script type="text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to delete data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>
<div>
    <asp:DropDownList runat="server" ID="ddlCountry">
    </asp:DropDownList>
    <asp:Button ID="btnConfirm" runat="server" OnClick="Delete" Text="Delete" OnClientClick="Confirm()" />
</div>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindList();
    }
}

private void BindList()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Country FROM Customers", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    da.Fill(dt);
                    ddlCountry.DataTextField = "Country";
                    ddlCountry.DataValueField = "CustomerId";
                    ddlCountry.DataSource = dt;
                    ddlCountry.DataBind();
                    ddlCountry.Items.Insert(0, new ListItem("--Select Items--", "0"));
                }
            }
        }
    }
}

protected void Delete(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];

    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE Country = @Country", con))
        {
            cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Text);
            if (confirmValue == "Yes")
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
            }
            this.BindList();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindList()
    End If
End Sub

Private Sub BindList()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Country FROM Customers", con)
            Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    da.Fill(dt)
                    ddlCountry.DataTextField = "Country"
                    ddlCountry.DataValueField = "CustomerId"
                    ddlCountry.DataSource = dt
                    ddlCountry.DataBind()
                    ddlCountry.Items.Insert(0, New ListItem("--Select Items--", "0"))
                End Using
            End Using
        End Using
    End Using
End Sub

Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
    Dim confirmValue As String = Request.Form("confirm_value")
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString

    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("DELETE FROM Customers WHERE Country = @Country", con)
            cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Text)
            If confirmValue = "Yes" Then
                Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked YES!')", True)
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked NO!')", True)
            End If
            Me.BindList()
        End Using
    End Using
End Sub

Screenshot