How to ask yes or no before delete to the website user first in asp.net web application(.Net framework) c#?

Below are the deletion part of my current code for the forummanagement.aspx.cs:

if (checkIfPendingForumExists())
{
    try
    {
        SqlConnection con = new SqlConnection(strcon);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        SqlCommand cmd = new SqlCommand("DELETE from forum_pending_tbl WHERE forum_id='" + TextBox1.Text.Trim() + "'", con);

        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script>alert('Forum Deleted Successfully');</script>");

        GridView1.DataBind();

    }
    catch (Exception ex)
    {
        Response.Write("<script>alert('" + ex.Message + "');</script>");
    }
}
else
{
    Response.Write("<script>alert('Invalid Forum ID');</script>");
}

This is the updated code for forummanagement.aspx:

<div class="col-4">
    <asp:Button ID="Button2" class="btn btn-lg btn-block btn-danger" runat="server" Text="Delete" ButtonID.OnClientClick='return confirm("Are you sure you want to delete this item?");'  />
</div>

After the updated buttonId.onclientclick suggested: there was error showing this:

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The server tag is not well formed.

Source Error:

Line 146:                     </div>
Line 147:                     <div class="col-4">
Line 148:                        <asp:Button ID="Button2" class="btn btn-lg btn-block btn-danger" runat="server" Text="Delete" ButtonID.OnClientClick='return confirm("Are you sure you want to delete this item?");'  />
Line 149:                     </div>
Line 150:                  </div>

Source File: /adminforummanagement.aspx Line: 148

As you can see, I haven’t able to ask the user to confirm the deletion first before actually directly deleting it because i’m not sure how to do that. Is there some kind of Response.”something” that can be used to confirm the deletion. I may not know the syntax for that code or the response.write be written differently to get the use input Y or N?
Im very new to asp.net and hope i can get any Help which would be appreciated.

Im using Asp.net web application (.NET Framework) c# as my project.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your button has the error Parser Error Message: The server tag is not well formed.

Your markup:

<asp:Button ID="Button2" class="btn btn-lg btn-block btn-danger" 
    runat="server" 
    Text="Delete" 
    // error here:
    ButtonID.OnClientClick='return confirm("Are you sure you want to delete this item?");' />

you only need OnClientClick, without ButtonID:
<asp:Button ID="Button2" class="btn btn-lg btn-block btn-danger" 
    runat="server" 
    Text="Delete" 
    OnClientClick='return confirm("Are you sure you want to delete this item?");' />

Method 2

Line 148 you don’t need to include the ID below should work (look at the single and double quotes placement too. You only would need the ID from C# side (code behind)

<asp:Button ID="Button2" class="btn btn-lg btn-block btn-danger" 
    runat="server" 
    Text="Delete" 
    OnClientClick="return confirm('Are you sure you want to delete this item?');" />


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x