Before deleting a user's operation record, a prompt is sent to the user to confirm whether to delete the record. In general, it is no problem to delete a record. However, you can select multiple records if you do not select a record. If you click Delete button, the user will first prompt the confirmation information before deletion, and then prompt that the user has not selected the record to be deleted.
Insus. NET wants to achieve is to change its order, first judge whether the user has selected a record, if not, prompt the user to select a record, once there is a selected record, to confirm the deletion.
In the following example, a CheckBox is placed in the first example of the GridView, allowing the user to select multiple records, and then put a Delete button outside the Gridview.
Demo: http://www.jb51.net/article/33564.htm
Xxx. aspx:
Copy codeThe Code is as follows:
<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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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 to be deleted. ");
Return false;
}
If (confirm ("the record selected below will be deleted. ") = True)
Return true;
Else
Return false;
}