这是我做的你参考一下吧
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BackColor="#00cc66" BorderColor="#339966" BorderStyle="none" BorderWidth="1px"
CellPadding="1" ForeColor="black" GridLines="Vertical" OnItemCommand="dgitem"
OnPageIndexChanged="datagrid_page" PageSize="9" Width="500">
<SelectedItemStyle BackColor="white" Font-Bold="true" ForeColor="white" />
<AlternatingItemStyle BackColor="#CCFF99" />
<ItemStyle BackColor="White" />
<HeaderStyle BackColor="#99FF99" Font-Bold="True" ForeColor="Black" />
<FooterStyle BackColor="Black" />
<Columns>
<asp:BoundColumn DataField="note_num" Visible=false>
</asp:BoundColumn>
<asp:BoundColumn DataField="note_date" HeaderText="发布日期">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:BoundColumn DataField="note_name" HeaderText="公告主题">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:BoundColumn DataField="note_content" HeaderText="公告内容">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="编辑">
<ItemTemplate>
<asp:Button ID="buted" runat="server" CommandName="edit" CssClass="button" Text="编辑" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:Button ID="butdel" runat="server" CommandName="delete" CssClass="button" Text="删除" /></ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle BackColor="#99FF99" Mode="NumericPages" />
</asp:DataGrid>
public partial class Note : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCmd = "select * from lab_note";
if (!IsPostBack)
{
Comm1 note_co = new Comm1();
note_co.dg_Bind(strCmd,DataGrid1);
}
}
protected void datagrid_page(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
string strCmd1 = "select * from lab_note";
Comm1 note_pa = new Comm1();
note_pa.dg_Bind(strCmd1, DataGrid1);
}
public void dgitem(object sender, DataGridCommandEventArgs e)
{
int notenum = int.Parse(e.Item.Cells[0].Text);
string notename = e.Item.Cells[2].Text;
if (e.CommandName == "edit")
{
string str = "<script language='javascript' defer>ret=window.showModalDialog('Note_Add.aspx?Action=edit&Note_num=" + notenum + "',window,'dialogHeight:300px;dialogWidth:600px;center:yes;Help:No;Resizable:No;Scroll:auto;Status:No;');</script>";
Response.Write(str);
}
if (e.CommandName == "delete")
{
Response.Write("<script defer>confirm('确定要删除吗?');</script>");
string strCmd2 = "delete lab_note where note_num='" + notenum + "'";
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["labConnectionString"].ConnectionString);
SqlCommand myCommand = new SqlCommand(strCmd2, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
string strCmd3 = "select * from lab_note";
Comm1 note_bu = new Comm1();
note_bu.dg_Bind(strCmd3, DataGrid1);
}