Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.

I`m making a board using GridView, and I have some problem.

When I try to delete items that check box checked item, I want to like this.

if check box unchecked, and press delete button, alert message "Please check the items"

Or check box checked, and press delete button, confirm message "Check item will deleted!!"

and choose yes selected items are deleted.

How can I make my code? I tried below,

When check box unchecked, alert message shows well, But I try to delete checked items

there`s no confirm message, still confirm message showed.....

Please check my code and give me you advice.

What I have tried:

protected void lnbDel_Click(object sender, EventArgs e)
{
UserBoardDAC dac = new UserBoardDAC();

foreach (GridViewRow gRow in grvList.Rows)
{
CheckBox chk = (CheckBox)gRow.FindControl("chk");
if (chk.Checked)
{
string message = @"
<script type='text/javascript'>
var delConfirm = confirm('Check item will deleted!! Are you sure?');
if (delConfirm)
{
return true;
}
else
{
return false;
}
</script>";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", message);
int boardItemID = Convert.ToInt32(gRow.Cells[0].Text);
dac.DeleteSelectedContents(boardItemID);
}
else
{
string message = @"
<script type='text/javascript'>
alert('Please check the items');
</script>";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", message);
}
}
//PutGridView();
GetCustomPaging(1);

}
Posted
Updated 19-Aug-16 0:56am

Confirmation should be handled at the client-side. Here's a quick example:

Suppose that you have this GridView Columns declaration:
ASP.NET
<columns>
   <asp:templatefield xmlns:asp="#unknown">
   <HeaderTemplate>
      <asp:button id="ButtonDelete" runat="server" text="Delete" />
   </HeaderTemplate>
   <itemtemplate>
      <asp:checkbox id="CheckBox1" runat="server" />
   </itemtemplate>
   </asp:templatefield>
   <asp:boundfield datafield="CustomerID" headertext="ID" readonly="True" xmlns:asp="#unknown" />
   <asp:boundfield datafield="CompanyName" headertext="Company" xmlns:asp="#unknown" />
   <asp:boundfield datafield="ContactName" headertext="Name" xmlns:asp="#unknown" />
   <asp:boundfield datafield="ContactTitle" headertext="Title" xmlns:asp="#unknown" />
   <asp:boundfield datafield="Address" headertext="Address" xmlns:asp="#unknown" />
   <asp:boundfield datafield="Country" headertext="Country" xmlns:asp="#unknown" />
</columns> 


You could then write a JavaScript function to handle the confirmation like in the following:

JavaScript
<script type="text/javascript" language="javascript">
        function ConfirmOnDelete(item)
        {
		if(IsValidToDelete()){
          		if (confirm("The following item(s) will be deleted: " + item + "Continue?")==true)
            			return true;
          		else
            		return false;
		}
		else{
			alert("Please check an item.");
		}
        }

	function IsValidToDelete() {
    		var valid = false;
    		var gv = document.getElementById("<%=GridView1.ClientID%>");
    		for (var i = 0; i < gv.getElementsByTagName("input").length; i++) {
        		var node = gv.getElementsByTagName("input")[i];
        		if (node != null && node.type == "checkbox" && node.checked) {
            			valid = true;
            			break;
        		}
    		}

    		return valid;
	}
</script>


Now, you need to hook up a JavaScript onclick event for the Delete button to invoke the ConfirmOnDelete() function. To to do that, you could do it like this at RowDataBound event:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.Header) //check for RowType
        {
            //access the LinkButton from the Header TemplateField using FindControl
            Button b = (Button)e.Row.FindControl("ButtonDelete"); 
            //attach the JavaScript function
            b.Attributes.Add("onclick", "return ConfirmOnDelete();"); 
        }
}


Or the simplest way is to directly call the ConfirmOnDelete() function declaratively like this:
ASP.NET
<asp:button id="ButtonDelete" runat="server" text="Delete" xmlns:asp="#unknown">
	    OnClick="ButtonDelete_Click"
	    OnClientClick="return ConfirmOnDelete();" />
</asp:button>


Notice the call to ConfirmOnDelete() function in OnClientClick event.

For the server-side deletion, I would suggest you to refer this example: GridView Multiple Delete with CheckBox and Confirm[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 20-Aug-16 1:37am    
5! for your efforts
Vincent Maverick Durano 20-Aug-16 4:03am    
Thank you again, Karthik :)
Karthik_Mahalingam 20-Aug-16 4:07am    
welcome :)
RydenChoi 21-Aug-16 21:19pm    
Perfect Solution!!!
Finally I made it from your code and comment.
I really appreciate of you!! :)
Vincent Maverick Durano 22-Aug-16 8:32am    
I'm glad to be of help ;)
A few possible methods listed here

Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]

One thing you have to bear in mind is that when the user clicks the delete button and it goes into your server code, your server code can't execute code on the client so you can't have your server code send javascript to the client, wait for a button to be clicked, then continue on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900