Select record in the GridView and confirm deletion by the confirm user

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.