个人认为此页面包含了对gridview 的操作所有功能!
。aspx文件
<%@ Page Language="C#" CodeFile="xinxi_manage.aspx.cs" Inherits="web_leooeback_xinxi_manage" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<%@ Register Src="find_news.ascx" TagName="find_news" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="800" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="#FFFFFF">
<td align="center" colspan="2" > 免费发布信息列表
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" Width="800px" OnRowDeleting="GridView1_RowDeleting" EmptyDataText="暂时无数据" BorderColor="White" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemStyle Width="20px" />
<ItemTemplate>
<asp:CheckBox id="newsid" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="序号" >
<ItemStyle Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="标题">
<ItemStyle Width="400px" />
<ItemTemplate>
<a href="list.aspx?id=<%# eval_r("id") %>" target="_blank"><%#eval_r("title") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="发表时间">
<ItemStyle Width="100px" />
<ItemTemplate>
<%# Convert.ToDateTime(eval_r("addtime")).Date.ToString("yyyy-MM-dd") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemStyle Width="100px" />
<ItemTemplate>
<asp:Label ID="lbcheck" runat="server" Text="未审核"></asp:Label>
<asp:Button ID="btncheck" runat="server" Text="审核" CommandName="shenhe" CommandArgument="<%# Container.DataItemIndex %>"/>
<asp:Label ID="lbcheckok" runat="server" Text="已审核"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" DeleteText="<div id="de" οnclick="JavaScript:return confirm('确定删除吗?')">删除</div>" >
<ItemStyle Width="30px" />
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
<font color=red>暂时无数据</font>
</EmptyDataTemplate>
<RowStyle Height="20px" />
</asp:GridView>
<table width="800" border="0" >
<tr>
<td align="left">
<asp:Button ID="btndeleteall" runat="server" Text="删除选中" OnClick="btndeleteall_Click" />
</td>
</tr>
<tr>
<td align="center">
<webdiyer:aspnetpager id="AspNetPager1" runat="server" firstpagetext="首页" lastpagetext="最后一页"
nextpagetext="下一页" prevpagetext="上一页" width="416px" OnPageChanging="AspNetPager1_PageChanging" AlwaysShow="True" BorderColor="White"></webdiyer:aspnetpager>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
。cs 文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using System.Data.SqlClient;
public partial class web_leooeback_xinxi_manage : System.Web.UI.Page
{
protected static string sqlcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand("select count(*) from xinxi",con);
AspNetPager1.PageSize =Convert.ToInt32(cmd.ExecuteScalar().ToString());//设置分页的总页数
viewbind();
}
}
//绑定gridview分页
private void viewbind()
{
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlDataAdapter sdr = new SqlDataAdapter("select * from xinxi order by addtime desc",con);
DataSet ds = new DataSet();
sdr.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "xinxi");
GridView1.DataSource = ds.Tables[0].DefaultView;
string[] str ={ "id"};
GridView1.DataKeyNames = str;
GridView1.DataBind();
}
//批量删除
protected void btndeleteall_Click(object sender, EventArgs e)
{
string sqltext = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chb = (CheckBox)GridView1.Rows[i].FindControl("newsid");
if (chb.Checked)
{
sqltext = sqltext + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
sqltext = sqltext.Substring(0, sqltext.Length - 1) + ")";
sqltext = "delete from xinxi where id in" + sqltext;
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand(sqltext, con);
try
{
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
{
viewbind();
MessageBox.Show(this, "删除成功,共删除" + count + "条记录!");
}
}
catch
{
MessageBox.Show(this, "删除失败!");
}
finally
{
con.Close();
con.Dispose();
}
}
//设置审核的状态
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string id = e.Row.Cells[1].Text.ToString();
Label lbcheck = (Label)e.Row.FindControl("lbcheck");
Button btncheck = (Button)e.Row.FindControl("btncheck");
Label lbcheckok = (Label)e.Row.FindControl("lbcheckok");
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand("select isshenhe from xinxi where id='"+id+"'",con);
int isshenhe =int.Parse(cmd.ExecuteScalar().ToString());
//Response.Write(isshenhe);
con.Close();
con.Dispose();
if (isshenhe == 0)
{
lbcheck.Visible = true;
btncheck.Visible = true;
lbcheckok.Visible = false;
}
else
{
lbcheckok.Visible = true;
lbcheck.Visible = false;
btncheck.Visible = false;
}
}
}
// 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id =Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
B_xinxi B_xinxi = new B_xinxi();
B_xinxi.Delete(id);
viewbind();
MessageBox.Show(this, "删除成功!");
}
//分页事件
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
viewbind();
}
//审核事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "shenhe")
{
int index =Convert.ToInt32(e.CommandArgument);
string id= GridView1.Rows[index].Cells[1].Text.ToString();
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand("update xinxi set isshenhe='1' where id='"+id+"'",con);
cmd.ExecuteNonQuery();
viewbind();
MessageBox.Show(this, "审核通过!");
}
}
}