Disable OnClientClick event from code behind using C# and VB.Net in ASP.Net

fkz2899
 
on Oct 09, 2019 12:33 AM
Sample_638715.zip
2223 Views

When user delete the item so it will show Zero item in cart  even though if user clicks on place order button and it shows OnclientClick's Confirmation alert box.. so  i want to disable that OnClientClick when cart is empty.

<div style="text-align: center; width: 250px;margin-bottom:10px">
    <b><asp:Button ID="dd" class="btn btn-warning" Style="text-align: center; margin-left: 100%" runat="server" OnClick="dd_Click" OnClientClick="return confirm('Are you sure you want to Place Order?');"  Text="Place Order" /></b>
</div>

 

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    DataTable dt = new DataTable();
    dt = (DataTable)Session["buy"];
    for (int i = 0; i <= dt.Rows.Count - 1; i++)
    {
        int sr;
        int sr1;
        string qdata;
        string dtdata;
        sr = Convert.ToInt32(dt.Rows[i]["sno"].ToString());
        TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];
        qdata = cell.Text;
        dtdata = sr.ToString();
        sr1 = Convert.ToInt32(dtdata);

        if (sr == sr1)
        {
            dt.Rows[i].Delete();
            dt.AcceptChanges();


            if (Session["buy"] == null)
            {

                dd.Attributes.Add("OnClientClick", "return false;");

            }
            //Label1.Text = "Item Has Been Deleted From Shopping Cart";
            break;

        }
    }

    for (int i = 1; i <= dt.Rows.Count; i++)
    {
        dt.Rows[i - 1]["sno"] = i;
        dt.AcceptChanges();
    }
    Session["buy"] = dt;
    Response.Redirect("checkout.aspx");
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
pandeyism
 
on Oct 09, 2019 05:01 AM

Hi fkz2899,

Refer below sample.

HTML

<asp:GridView ID="GridView1" CssClass="Grid" runat="server" OnRowDeleting="GridView1_OnRowDeleting"
    AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Item" HeaderText="Item" />
        <asp:BoundField DataField="Price" HeaderText="Price" />
        <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
    </Columns>
</asp:GridView>
<div style="text-align: center; width: 250px; margin-bottom: 10px">
    <b>
        <asp:Button ID="dd" class="btn btn-warning" Style="text-align: center; margin-left: 100%"
            runat="server" OnClick="dd_Click" OnClientClick="return confirm('Are you sure you want to Place Order?');"
            Text="Place Order" /></b></div>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Price") });
        dt.Rows.Add("Shirt", 450);
        dt.Rows.Add("Jeans", 3200);
        dt.Rows.Add("Trousers", 1900);
        Session["buy"] = dt;
        BindGrid();
    }
}

protected void BindGrid()
{
    GridView1.DataSource = Session["buy"] as DataTable;
    GridView1.DataBind();
}

protected void GridView1_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    DataTable dt = new DataTable();
    dt = (DataTable)Session["buy"];
    dt.Rows[e.RowIndex].Delete();
    dt.AcceptChanges();
    if (dt.Rows.Count == 0)
    {
        dd.OnClientClick = null;
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string item = e.Row.Cells[0].Text;
        foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
        {
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
            }
        }
    }
}

protected void dd_Click(object sender, EventArgs e)
{

}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Item"), New DataColumn("Price")})
        dt.Rows.Add("Shirt", 450)
        dt.Rows.Add("Jeans", 3200)
        dt.Rows.Add("Trousers", 1900)
        Session("buy") = dt
        BindGrid()
    End If
End Sub

Protected Sub BindGrid()
    GridView1.DataSource = TryCast(Session("buy"), DataTable)
    GridView1.DataBind()
End Sub

Protected Sub GridView1_OnRowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
    Dim dt As DataTable = New DataTable()
    dt = CType(Session("buy"), DataTable)
    dt.Rows(e.RowIndex).Delete()
    dt.AcceptChanges()

    If dt.Rows.Count = 0 Then
        dd.OnClientClick = Nothing
    End If

    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim item As String = e.Row.Cells(0).Text
        For Each button As Button In e.Row.Cells(2).Controls.OfType(Of Button)()
            If button.CommandName = "Delete" Then
                button.Attributes("onclick") = "if(!confirm('Do you want to delete " & item & "?')){ return false; };"
            End If
        Next
    End If
End Sub

Protected Sub dd_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub