GridView后的"编辑"按钮(就是有编辑更新取消)如何操作来向数据库中更改数据,怎么写后台代码 通过GridView控件里面的就是Commandfield里面的编辑更新取消按钮来更新数据库求,跪求后台代码啊 求大神吗救救我啊 我刚学ASP.net啊 本人菜的狠啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.cnblogs.com/dushouke/archive/2008/03/24/1120377.html 连本入门书都不舍得买的话,可以看这个很古老的教程。这个教程出来的时候,连asp.net ajax都还不存在,所以他不涉及那部分。http://www.cnblogs.com/Reeezak/archive/2007/08/13/853925.html 有edit (ing,ed)update (ing,ed)cancel事件的 这是我写的一个公告编辑删除的一个程序;你看看吧!希望对你有帮助!! PS:学ASP.NET真的很痛苦啊!public partial class BulletinManage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView(); } } private void BindGridView() { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True"); string sqlStr = "select * from T_Bulletin"; SqlDataAdapter dap = new SqlDataAdapter(sqlStr, con); DataSet ds = new DataSet(); //con.Open(); dap.Fill(ds); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] { "BulletinID" }; GridView1.DataBind(); //con.Close(); //显示当前页数 lblPageSum.Text = "当前页为" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + "页"; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { //重新获得页索引 GridView1.PageIndex = e.NewPageIndex; BindGridView(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //设置日期格式 e.Row.Cells[3].Text = e.Row.Cells[3].Text != "" ? Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString() : "2011-10-24 "; //e.Row.Cells[3].Text = Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString(); //高亮显示指定行 e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#f6b33e'"); e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;"); //删除时弹出对话框 //((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm ('确定删除吗?')"); } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //删除公告 string sqlStr = "delete from T_Bulletin where BulletinID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True"); SqlCommand cmd = new SqlCommand(sqlStr, con); con.Open(); //执行SQL语句 cmd.ExecuteNonQuery(); con.Close(); BindGridView(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //新的编辑列索引指定给当前索引 GridView1.EditIndex = e.NewEditIndex; BindGridView(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { //把编辑索引设为-1 GridView1.EditIndex = -1; BindGridView(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //获取要更新字段的ID值 string BulletinID = GridView1.DataKeys[e.RowIndex].Value.ToString(); string Title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; string Content = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text; //编写修改的SQL语句 string strSql = "update T_Bulletin set Title='"+Title +"',Content='"+Content +"'where BulletinID='"+BulletinID +"'"; //执行修改操作 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True"); //初始化执行命令 SqlCommand cmd = new SqlCommand(strSql ,con ); con.Open(); cmd.ExecuteNonQuery(); con.Close(); //取消编辑状态 GridView1.EditIndex = -1; //重新绑定 BindGridView(); } } 删除的时候如何在前端判断repeater内checkbox是否被选中? 如何才能根据选中的值,传参然后赋值 论坛首页如何读取数据一些内容 如何在让iframe百分百填充Panel? 求图片加文字水印(需描边的)代码 jquery ajax 乱码问题 DropDownlist的问题 新手提问: 我安装了.net 2003,用什么软件编写asp.net网页? DataGrid,分析数据显示的问题,Please "缺少标识符“错误 gridview合并 数据绑定控件,例如GridView是不是只能绑定DataTable类型的数据?还能绑定其他类型的吗
edit (ing,ed)
update (ing,ed)
cancel事件的
public partial class BulletinManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
} private void BindGridView()
{ SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True");
string sqlStr = "select * from T_Bulletin";
SqlDataAdapter dap = new SqlDataAdapter(sqlStr, con);
DataSet ds = new DataSet();
//con.Open();
dap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "BulletinID" };
GridView1.DataBind();
//con.Close();
//显示当前页数
lblPageSum.Text = "当前页为" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + "页"; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//重新获得页索引
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{ //设置日期格式
e.Row.Cells[3].Text = e.Row.Cells[3].Text != "" ? Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString() : "2011-10-24 "; //e.Row.Cells[3].Text = Convert.ToDateTime(e.Row.Cells[3].Text).ToShortDateString(); //高亮显示指定行
e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#f6b33e'");
e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;"); //删除时弹出对话框
//((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm ('确定删除吗?')");
}
} protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//删除公告
string sqlStr = "delete from T_Bulletin where BulletinID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True");
SqlCommand cmd = new SqlCommand(sqlStr, con);
con.Open(); //执行SQL语句
cmd.ExecuteNonQuery();
con.Close();
BindGridView();
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//新的编辑列索引指定给当前索引
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
} protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//把编辑索引设为-1
GridView1.EditIndex = -1;
BindGridView();
} protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//获取要更新字段的ID值
string BulletinID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string Content = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text; //编写修改的SQL语句
string strSql = "update T_Bulletin set Title='"+Title +"',Content='"+Content +"'where BulletinID='"+BulletinID +"'"; //执行修改操作
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog='Trading Site';Integrated Security=True"); //初始化执行命令
SqlCommand cmd = new SqlCommand(strSql ,con ); con.Open();
cmd.ExecuteNonQuery();
con.Close(); //取消编辑状态
GridView1.EditIndex = -1; //重新绑定
BindGridView(); }
}