GridView selects the record and confirm user confirms the deletion

  • 2020-05-24 05:28:46
  • OfStack

User operation before deleting the record to the user 1 to confirm whether to delete the prompt. In general, there is no problem with one record being deleted for every record. However, if multiple options are available, if the user does not select the record, directly click the delete ammonium button, it will first prompt the confirmation information before the deletion, and then prompt the user not to select the record he wants to delete.

What Insus.NET wants to achieve is to switch its order by 1, and first judge whether the user has selected the record. If not, the user will be prompted to select the record.

In the following example, the first GridView example will place an CheckBox, let the user select multiple records, and then put an Delete ammonium button outside Gridview.
Presentation: https: / / www. ofstack. com article / 33564. htm
xxx.aspx:
 
<asp:Button ID="ButtonDelete" Text="Delete" runat="Server" OnClick="ButtonDelete_Click" 
CausesValidation="false" /> 
<asp:GridView ID="GvCutterType" runat="server" DataKeyNames="CutterTypeId" AutoGenerateColumns="false"> 
<Columns> 
<asp:TemplateField> 
<ItemStyle BorderWidth="1" BorderColor="#c0c0c0" Width="1%" /> 
<ItemTemplate> 
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Javascript:changeRowBgColor(this)" /> 
</ItemTemplate> 
</asp:TemplateField> 
<!-- 
other column templateField 
--> 
</Columns> 
</asp:GridView> 

xxx.aspx.cs:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
this.ButtonDelete.Attributes.Add("onclick", "return ConfirmOnDelete();"); 
} 
protected void ButtonDelete_Click(object sender, EventArgs e) 
{ 
//do delete something 
} 

Javascript:
 
function ConfirmOnDelete() { 
if (document.getElementById("<%= GvCutterType.ClientID %>") == null) { 
return false; 
} 
var objgv = document.getElementById("<%= GvCutterType.ClientID %>"); 
var rc = objgv.rows.length; 
var y = 0; 
for (var i = 0; i < rc; i++) { 
var getInput = objgv.rows[i].cells[0].getElementsByTagName("input"); 
if (getInput[0].type == "checkbox") { 
if (getInput[0].checked) { 
y = y + 1; 
} 
} 
} 
if (y <= 0) { 
alert(" First select the record you want to delete. "); 
return false; 
} 
if (confirm(" The following selected records will be deleted. ") == true) 
return true; 
else 
return false; 
} 

Related articles: