GridView 批量删除,自定义分页,定位页码
程序代码
前台代码:
<table width="100%" height="20" border="1" align="center" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF" bordercolordark="#E6E6E6" bgcolor="#FFFFFF">
<tr bgcolor="#FFFFFF">
<td width="7%" align="center">
<asp:CheckBox ID="cbAll" runat="server" AutoPostBack="True" OnCheckedChanged="cbAll_CheckedChanged" /></td>
<td width="8%" align="center"><img src="images/bz.gif" width="6" height="11"></td>
<td width="9%" align="center"><img src="images/mail.gif" width="14" height="11"></td>
<td width="16%" align="center">发件人</td>
<td width="39%" align="center">标题</td>
<td width="21%" align="center">日期</td>
</tr></table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4"
DataKeyNames="id" HorizontalAlign="Center"
Width="100%" BorderStyle="None" ShowHeader="False" AllowPaging="True" OnDataBound="GridView1_DataBound">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:TemplateField >
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:CheckBox ID=cb runat=server />
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField HeaderImageUrl="images/bz.gif">
<ItemStyle Width="8%" />
</asp:ImageField>
<asp:TemplateField HeaderImageUrl="images/mail.gif">
<ItemTemplate>
<img src=<%# Eval("view").ToString()=="0"?"images/mailread.gif":"images/mail.gif" %> border=0 align=middle />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="9%" />
</asp:TemplateField>
<asp:BoundField DataField="email" HeaderText="发件人">
<ItemStyle Width="16%" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="viewmail.aspx?id={0}"
DataTextField="title" HeaderText="标题" >
<ItemStyle Width="39%" />
</asp:HyperLinkField>
<asp:BoundField DataField="date" DataFormatString="{0:d}" HeaderText="日期">
<ItemStyle Width="21%" />
</asp:BoundField>
</Columns>
<PagerTemplate>
</PagerTemplate>
<SelectedRowStyle BackColor="#FFCC66" ForeColor="#663399" Font-Bold="True" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<RowStyle BackColor="White" ForeColor="#330099" />
</asp:GridView>
<table width="100%" height="20" border="1" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF" bordercolordark="#E6E6E6" bgcolor="#FFFFFF">
<tr><td>
<asp:Button ID="Button1" runat="server" Text="全选" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="删除" OnClick="Button2_Click" /></td>
<td align=right>
<asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>页</td>
</tr></table>
<table width="100%" height="20" border="1" align="center" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF" bordercolordark="#E6E6E6" bgcolor="#FFFFFF">
<tr bgcolor="#FFFFFF">
<td width="7%" align="center">
<asp:CheckBox ID="cbAll" runat="server" AutoPostBack="True" OnCheckedChanged="cbAll_CheckedChanged" /></td>
<td width="8%" align="center"><img src="images/bz.gif" width="6" height="11"></td>
<td width="9%" align="center"><img src="images/mail.gif" width="14" height="11"></td>
<td width="16%" align="center">发件人</td>
<td width="39%" align="center">标题</td>
<td width="21%" align="center">日期</td>
</tr></table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4"
DataKeyNames="id" HorizontalAlign="Center"
Width="100%" BorderStyle="None" ShowHeader="False" AllowPaging="True" OnDataBound="GridView1_DataBound">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:TemplateField >
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:CheckBox ID=cb runat=server />
</ItemTemplate>
</asp:TemplateField>
<asp:ImageField HeaderImageUrl="images/bz.gif">
<ItemStyle Width="8%" />
</asp:ImageField>
<asp:TemplateField HeaderImageUrl="images/mail.gif">
<ItemTemplate>
<img src=<%# Eval("view").ToString()=="0"?"images/mailread.gif":"images/mail.gif" %> border=0 align=middle />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="9%" />
</asp:TemplateField>
<asp:BoundField DataField="email" HeaderText="发件人">
<ItemStyle Width="16%" />
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="viewmail.aspx?id={0}"
DataTextField="title" HeaderText="标题" >
<ItemStyle Width="39%" />
</asp:HyperLinkField>
<asp:BoundField DataField="date" DataFormatString="{0:d}" HeaderText="日期">
<ItemStyle Width="21%" />
</asp:BoundField>
</Columns>
<PagerTemplate>
</PagerTemplate>
<SelectedRowStyle BackColor="#FFCC66" ForeColor="#663399" Font-Bold="True" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<RowStyle BackColor="White" ForeColor="#330099" />
</asp:GridView>
<table width="100%" height="20" border="1" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF" bordercolordark="#E6E6E6" bgcolor="#FFFFFF">
<tr><td>
<asp:Button ID="Button1" runat="server" Text="全选" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="删除" OnClick="Button2_Click" /></td>
<td align=right>
<asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>页</td>
</tr></table>
程序代码
后台代码:
void DataBinds()
{
if (!opMail.ReceiveGetList(userid))
{
base.MessageBox(string.Empty, opMail.strErrMsg);
}
else
{
this.GridView1.DataSource = opMail.dataView;
this.GridView1.DataBind();
}
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GridView1.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
}
//全选checkbox
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("cb")).Checked = true;
}
}
//删除所选
protected void Button2_Click(object sender, EventArgs e)
{
for (int rowindex = 0; rowindex < this.GridView1.Rows.Count; rowindex++)
{
if (((CheckBox)this.GridView1.Rows[rowindex].Cells[0].FindControl("cb")).Checked == true)
{
//删除Convert.ToInt32(this.GridView1.DataKeys[rowindex].Value)
if (!opMail.ReceiveDelete(Convert.ToInt32(this.GridView1.DataKeys[rowindex].Value)))
{
base.MessageBox(string.Empty, opMail.strErrMsg);
}
}
}
DataBinds();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
DataBinds();
}
protected void cbAll_CheckedChanged(object sender, EventArgs e)
{
if (this.cbAll.Checked == true)
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("cb")).Checked = true;
}
}
else
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("cb")).Checked = false;
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
DataBinds();
}
protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = 0;
DataBinds();
}
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex > 0)
{
this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
DataBinds();
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex < this.GridView1.PageCount)
{
this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
DataBinds();
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.GridView1.PageCount;
DataBinds();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount);
}
void DataBinds()
{
if (!opMail.ReceiveGetList(userid))
{
base.MessageBox(string.Empty, opMail.strErrMsg);
}
else
{
this.GridView1.DataSource = opMail.dataView;
this.GridView1.DataBind();
}
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GridView1.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
}
//全选checkbox
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("cb")).Checked = true;
}
}
//删除所选
protected void Button2_Click(object sender, EventArgs e)
{
for (int rowindex = 0; rowindex < this.GridView1.Rows.Count; rowindex++)
{
if (((CheckBox)this.GridView1.Rows[rowindex].Cells[0].FindControl("cb")).Checked == true)
{
//删除Convert.ToInt32(this.GridView1.DataKeys[rowindex].Value)
if (!opMail.ReceiveDelete(Convert.ToInt32(this.GridView1.DataKeys[rowindex].Value)))
{
base.MessageBox(string.Empty, opMail.strErrMsg);
}
}
}
DataBinds();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
DataBinds();
}
protected void cbAll_CheckedChanged(object sender, EventArgs e)
{
if (this.cbAll.Checked == true)
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("cb")).Checked = true;
}
}
else
{
foreach (GridViewRow row in GridView1.Rows)
{
((CheckBox)row.Cells[0].FindControl("cb")).Checked = false;
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
DataBinds();
}
protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = 0;
DataBinds();
}
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex > 0)
{
this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
DataBinds();
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GridView1.PageIndex < this.GridView1.PageCount)
{
this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
DataBinds();
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.GridView1.PageCount;
DataBinds();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount);
}