1.前台代码:
<div>
<asp:GridView ID="GVZhaoPin" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ZhaoPinID" OnLoad="GVZhaoPin_Load" OnRowCancelingEdit="GVZhaoPin_RowCancelingEdit" OnRowCommand="GVZhaoPin_RowCommand" OnRowDataBound="GVZhaoPin_RowDataBound" OnRowDeleting="GVZhaoPin_RowDeleting" OnRowEditing="GVZhaoPin_RowEditing">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:BoundField DataField="ZhaoPinID" />
<asp:BoundField DataField="ZhaoPinTitle" HeaderText="标题" />
<asp:BoundField DataField="ZhaoPinTime" HeaderText="发布时间">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="发布人" />
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("ZhaoPinID") %>' CommandName="Delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<webdiyer:aspnetpager id="AspNetPager1" runat="server"
horizontalalign="Center" onpagechanged="AspNetPager1_PageChanged"
width="50%"
CustomInfoHTML="总记录数:%RecordCount%,总页数:%PageCount%,当前为第%CurrentPageIndex%页"
PagingButtonSpacing="8px"
ShowCustomInfoSection="Right" Font-Size="12px" LayoutType="Table"
ShowNavigationToolTip="True" UrlPageIndexName="pageindex" UrlPaging="True"
PageIndexBoxType="TextBox" PageSize="5" ShowPageIndexBox="Auto"
SubmitButtonText="Go" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到" CustomInfoTextAlign="Left" >
</webdiyer:aspnetpager>
</div>
2.后台代码
protected void GVZhaoPin_RowCommand(object sender, GridViewCommandEventArgs e)
{ if (e.CommandName == "Delete")
{
int deleteid = int.Parse(e.CommandArgument.ToString());//GridViw总共有5页,如果我点第3页第一行的删除按钮,获取的却是第1页第一行的主键ID,后面非第1页的操作都是获取第一页的主键,这是为什么呢?
int deleteok = myBllZhaoPin_Admin.ZhaoPin_Delete(deleteid);
if (deleteok > 0)
{
ScriptManager.RegisterStartupScript(GVZhaoPin, this.GetType(), "", "alert('删除成功!')", true);
GVZhaoPin.EditIndex = -1;
//重新绑定
GVZhaoPin.DataSource = myBllZhaoPin_Admin.GVZhaoPin_Load();
GVZhaoPin.DataBind();
}
}
}
<div>
<asp:GridView ID="GVZhaoPin" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ZhaoPinID" OnLoad="GVZhaoPin_Load" OnRowCancelingEdit="GVZhaoPin_RowCancelingEdit" OnRowCommand="GVZhaoPin_RowCommand" OnRowDataBound="GVZhaoPin_RowDataBound" OnRowDeleting="GVZhaoPin_RowDeleting" OnRowEditing="GVZhaoPin_RowEditing">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:BoundField DataField="ZhaoPinID" />
<asp:BoundField DataField="ZhaoPinTitle" HeaderText="标题" />
<asp:BoundField DataField="ZhaoPinTime" HeaderText="发布时间">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="发布人" />
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("ZhaoPinID") %>' CommandName="Delete">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<webdiyer:aspnetpager id="AspNetPager1" runat="server"
horizontalalign="Center" onpagechanged="AspNetPager1_PageChanged"
width="50%"
CustomInfoHTML="总记录数:%RecordCount%,总页数:%PageCount%,当前为第%CurrentPageIndex%页"
PagingButtonSpacing="8px"
ShowCustomInfoSection="Right" Font-Size="12px" LayoutType="Table"
ShowNavigationToolTip="True" UrlPageIndexName="pageindex" UrlPaging="True"
PageIndexBoxType="TextBox" PageSize="5" ShowPageIndexBox="Auto"
SubmitButtonText="Go" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到" CustomInfoTextAlign="Left" >
</webdiyer:aspnetpager>
</div>
2.后台代码
protected void GVZhaoPin_RowCommand(object sender, GridViewCommandEventArgs e)
{ if (e.CommandName == "Delete")
{
int deleteid = int.Parse(e.CommandArgument.ToString());//GridViw总共有5页,如果我点第3页第一行的删除按钮,获取的却是第1页第一行的主键ID,后面非第1页的操作都是获取第一页的主键,这是为什么呢?
int deleteok = myBllZhaoPin_Admin.ZhaoPin_Delete(deleteid);
if (deleteok > 0)
{
ScriptManager.RegisterStartupScript(GVZhaoPin, this.GetType(), "", "alert('删除成功!')", true);
GVZhaoPin.EditIndex = -1;
//重新绑定
GVZhaoPin.DataSource = myBllZhaoPin_Admin.GVZhaoPin_Load();
GVZhaoPin.DataBind();
}
}
}
this.GridView1.DataBind();删除的时候string id = this.GridView1.DataKeys[e.Item.ItemIndex].ToString();
"Delete From 表名 Where id='" + id + "'"