<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
DataSourceID
=
"
AccessDataSource1
"
AllowPaging
=
"
True
"
AllowSorting
=
"
True
"
DataKeyNames
=
"
id
"
Height
=
"
174px
"
Width
=
"
597px
"
OnRowCommand
=
"
GridView1_RowCommand
"
OnSelectedIndexChanging
=
"
GridView1_SelectedIndexChanging
"
OnRowEditing
=
"
GridView1_RowEditing
"
OnSelectedIndexChanged
=
"
GridView1_SelectedIndexChanged
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
CellPadding
=
"
4
"
ForeColor
=
"
#333333
"
GridLines
=
"
None
"
>
< Columns >
< asp:TemplateField HeaderText = " 删除 " >
< ItemTemplate >
< asp:LinkButton ID = " Button2 " runat = " server " CausesValidation = " False " CommandName = " del " Text = " 删除 " CommandArgument = ' <%# Eval("id") %> ' />
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField HeaderText = " 编辑 " >
< ItemTemplate >
< asp:LinkButton ID = " Button1 " runat = " server " CausesValidation = " False " CommandName = " edit " Text = " 编辑 " CommandArgument = ' <%# Eval("id") %> ' />
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " title " HeaderText = " 文章标题 " SortExpression = " title " />
< asp:BoundField DataField = " Itime " HeaderText = " 修改时间 " SortExpression = " Itime " />
< asp:BoundField DataField = " type " HeaderText = " 类型 " SortExpression = " type " />
< asp:BoundField DataField = " id " HeaderText = " 编号 " InsertVisible = " False " ReadOnly = " True "
SortExpression = " id " />
</ Columns >
< FooterStyle BackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< RowStyle BackColor = " #F7F6F3 " ForeColor = " #333333 " />
< EditRowStyle BackColor = " #999999 " />
< SelectedRowStyle BackColor = " #E2DED6 " Font - Bold = " True " ForeColor = " #333333 " />
< PagerStyle BackColor = " #284775 " ForeColor = " White " HorizontalAlign = " Center " />
< HeaderStyle BackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< AlternatingRowStyle BackColor = " White " ForeColor = " #284775 " />
</ asp:GridView >
< Columns >
< asp:TemplateField HeaderText = " 删除 " >
< ItemTemplate >
< asp:LinkButton ID = " Button2 " runat = " server " CausesValidation = " False " CommandName = " del " Text = " 删除 " CommandArgument = ' <%# Eval("id") %> ' />
</ ItemTemplate >
</ asp:TemplateField >
< asp:TemplateField HeaderText = " 编辑 " >
< ItemTemplate >
< asp:LinkButton ID = " Button1 " runat = " server " CausesValidation = " False " CommandName = " edit " Text = " 编辑 " CommandArgument = ' <%# Eval("id") %> ' />
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField = " title " HeaderText = " 文章标题 " SortExpression = " title " />
< asp:BoundField DataField = " Itime " HeaderText = " 修改时间 " SortExpression = " Itime " />
< asp:BoundField DataField = " type " HeaderText = " 类型 " SortExpression = " type " />
< asp:BoundField DataField = " id " HeaderText = " 编号 " InsertVisible = " False " ReadOnly = " True "
SortExpression = " id " />
</ Columns >
< FooterStyle BackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< RowStyle BackColor = " #F7F6F3 " ForeColor = " #333333 " />
< EditRowStyle BackColor = " #999999 " />
< SelectedRowStyle BackColor = " #E2DED6 " Font - Bold = " True " ForeColor = " #333333 " />
< PagerStyle BackColor = " #284775 " ForeColor = " White " HorizontalAlign = " Center " />
< HeaderStyle BackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< AlternatingRowStyle BackColor = " White " ForeColor = " #284775 " />
</ asp:GridView >
在一个模板列中放一个删除的按钮,
<asp:LinkButton ID="Button2" runat="server" CausesValidation="False" CommandName="del" Text="删除" CommandArgument=' <%# Eval("id") %>
这里的commandName很重要,是后代区分功能的参数。CommandArgument是传递操作对象的参数。然后再GridView1_ROwCommand()事件中对不同的操作做处理。但是在实际的应用中好像速度有点。
protected
void
GridView1_RowCommand(
object
sender, GridViewCommandEventArgs e)
... {
if(e.CommandName=="edit")
...{
string id = e.CommandArgument.ToString();
string url="addnews.aspx?id="+id;
Response.Redirect(url);
//string sql = "delete from news where id='"+id+"'";
//OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
//OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
//myCommand.ExecuteNonQuery();
}
if (e.CommandName == "del")
...{
string id = e.CommandArgument.ToString();
OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
MyConnection.Open();
try
...{
string sql = "delete from news where id="+ id ;
string sql2 = "select * from news";
//OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
OleDbCommand MyCommand = new OleDbCommand(sql, MyConnection);
MyCommand.ExecuteNonQuery();
Response.Redirect("Managernews.aspx");
//OleDbDataAdapter myAdatpter = new OleDbDataAdapter(sql2, MyConnection);
//DataSet ds = new DataSet();
//myAdatpter.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}
catch (Exception ex)
...{
Response.Write(ex.Message);
}
finally
...{
MyConnection.Close();
}
}
}
... {
if(e.CommandName=="edit")
...{
string id = e.CommandArgument.ToString();
string url="addnews.aspx?id="+id;
Response.Redirect(url);
//string sql = "delete from news where id='"+id+"'";
//OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
//OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
//myCommand.ExecuteNonQuery();
}
if (e.CommandName == "del")
...{
string id = e.CommandArgument.ToString();
OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
MyConnection.Open();
try
...{
string sql = "delete from news where id="+ id ;
string sql2 = "select * from news";
//OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
OleDbCommand MyCommand = new OleDbCommand(sql, MyConnection);
MyCommand.ExecuteNonQuery();
Response.Redirect("Managernews.aspx");
//OleDbDataAdapter myAdatpter = new OleDbDataAdapter(sql2, MyConnection);
//DataSet ds = new DataSet();
//myAdatpter.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}
catch (Exception ex)
...{
Response.Write(ex.Message);
}
finally
...{
MyConnection.Close();
}
}
}