Specific methods for customizing the delete operation of the GridView

Source: Internet
Author: User

First, the front-end code is as follows:

Copy codeThe Code is as follows:
<Asp: GridView ID = "gridViewDxjk" CssClass = "gridview" runat = "server" AllowPaging = "True"
DataKeyNames = "P_ID" AutoGenerateColumns = "False"
RowStyle-HorizontalAlign = "Center" BorderWidth = "1px" PageSize = "17"
Onrowdeleting = "gridViewDxjk_RowDeleting"
OnRowDataBound = "gridViewDxjk_RowDataBound"
Onpageindexchanging = "gridViewDxjk_PageIndexChanging">
<HeaderStyle CssClass = "head"/>
<PagerStyle CssClass = "pager"/>
<RowStyle CssClass = "row"/>
<EditRowStyle CssClass = "editrow"/>
<AlternatingRowStyle CssClass = "altrow"/>
<EmptyDataRowStyle CssClass = "empty"/>
<Columns>
<Asp: HyperLinkField HeaderText = "edit" ControlStyle-Width = "50" DataNavigateUrlFields = "P_ID" DataNavigateUrlFormatString = "smsModify. aspx? Id = {0} "Text =" edit ">
<ControlStyle Width = "50px"> </ControlStyle> </asp: HyperLinkField>
<Asp: CommandField ShowDeleteButton = "true" DeleteText = "delete">
<ControlStyle Width = "50px"> </ControlStyle> </asp: CommandField>
<Asp: BoundField DataField = "P_ID" HeaderText = "id" SortExpression = "P_ID" ItemStyle-HorizontalAlign = "Center" Visible = "False">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_NAME" HeaderText = "name" SortExpression = "P_NAME"/>
<Asp: BoundField DataField = "P_Type" HeaderText = "notification method" SortExpression = "P_Type" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_Fzr" HeaderText = "name" SortExpression = "P_Fzr" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_tel" HeaderText = "Notification mobile phone" SortExpression = "P_tel" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_jg" HeaderText = "notification interval (hours)" SortExpression = "P_jg" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_on" HeaderText = "enable" SortExpression = "P_on" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_lasttime" HeaderText = "last sending time" SortExpression = "P_lasttime" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
<Asp: BoundField DataField = "P_memo" HeaderText = "Remarks" SortExpression = "P_memo" ItemStyle-HorizontalAlign = "Center">
<ItemStyle HorizontalAlign = "Center"> </ItemStyle> </asp: BoundField>
</Columns>
<EmptyDataTemplate>
No data!
</EmptyDataTemplate>
<PagerTemplate>
<Table width = "100%" class = "gvPage" style = "font-size: 12px;">
<Tr>
<Td style = "text-align: right">
<Asp: Label ID = "lblPageIndex" runat = "server" Text = '<% # (GridView) Container. parent. parent ). pageIndex + 1%> '> </asp: Label> page
/Total <asp: Label ID = "lblPageCount" runat = "server" Text = '<% # (GridView) Container. parent. parent ). pageCount %> '> </asp: Label> page
<Asp: LinkButton ID = "LinkButtonFirstPage" runat = "server" CommandArgument = "First" CommandName = "Page" Visible = "<% # (GridView) Container. namingContainer ). pageIndex! = 0%> "> homepage </asp: LinkButton>
<Asp: LinkButton ID = "LinkButtonPreviousPage" runat = "server" CommandArgument = "Prev" CommandName = "Page" Visible = "<% # (GridView) Container. namingContainer ). pageIndex! = 0%> "> previous page </asp: LinkButton>
<Asp: LinkButton ID = "LinkButtonNextPage" runat = "server" CommandArgument = "Next" CommandName = "Page" Visible = "<% # (GridView) Container. namingContainer ). pageIndex! = (GridView) Container. NamingContainer). PageCount-1%> "> next page </asp: LinkButton>
<Asp: LinkButton ID = "LinkButtonLastPage" runat = "server" CommandArgument = "Last" CommandName = "Page" Visible = "<% # (GridView) Container. namingContainer ). pageIndex! = (GridView) Container. NamingContainer). PageCount-1%> "> last page </asp: LinkButton>
<Asp: TextBox ID = "txtNewPageIndex" runat = "server" Text = '<% # (GridView) Container. parent. parent ). pageIndex + 1%> 'width = "20px" AutoPostBack = "true"> </asp: TextBox>
<Asp: LinkButton ID = "btnGoEx" runat = "server" CommandArgument = "GO" CommandName = "Page" Text = "GO" OnClick = "btnGoEx_Click"> </asp: linkButton>
</Td>
</Tr>
</Table>
</PagerTemplate>
</Asp: GridView>

The confirmation box before deletion needs to pop up at the backend. So what do we need to do in RowDataBound? In addition, to delete a row, you must trigger the RowDeleting event. The specific code is as follows:

Copy codeThe Code is as follows:
// Delete an alarm
Protected void gridViewDxjk_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
String key = gridViewDxjk. DataKeys [e. RowIndex]. Value. ToString ();
Bool flag = bll. Delete (Int32.Parse (key ));
If (flag)
NXT_WLService.App_Code.JScript.Alert ("deleted successfully! ", This );
Else
NXT_WLService.App_Code.JScript.Alert ("deletion failed! ", This );
}


Protected void gridViewDxjk_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
LinkButton btn = (LinkButton) e. Row. Cells [1]. Controls [0];
If (btn. Text. Equals ("delete") // a message is displayed when the cursor is exceeded.
Btn. OnClientClick = "if (confirm ('Are you sure you want to delete? ') Javascript :__ doPostBack ('gridviewdxjk', 'delete $ "+ e. Row. RowIndex. ToString () +" '); else return false ;";
}
}

Related Article

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.