太原工业学院毕业设计(论文)
图4.5 公告管理页面的运行效果
在Web窗体中添加GridView控件,将字段对话框中添加绑定字段、命令字段中的“编辑、更新、取消”字段,用于编辑公告内容的修改,添加命令字段中的“删除”字段用于将所选的公告内容删除。
GridView控件的属性设置,可以通过前台代码实现,也可以在“控件属性”对话框中设置实现,本页中使用的控件属性设置请参见如下代码。 CellPadding=\ForeColor=\GridLines=\OnRowCancelingEdit=\w1_RowDeleting\dView1_RowUpdating\ DataField=\ HeaderText=\ 内 容 \ /> HeaderText=\ 编 辑 \ ShowEditButton=\ /> BackColor=\ ForeColor=\ HorizontalAlign=\ 37 太原工业学院毕业设计(论文) BackColor=\/> 在Page_Load事件中编写如下代码,用于连接数据库绑定GridView控件的数据源,代码如下: SqlConnection strcon = new SqlConnection( System.Configuration.ConfigurationManager.AppSettings[\连接数据库 DataSet dset = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (Session[\ { Server.Transfer(\ } strcon.Close(); if (!IsPostBack) { dsdatabind(); } } protected void dsdatabind() //绑定GridView1数据 { strcon.Open(); string newque = \ SqlDataAdapter datap = new SqlDataAdapter(newque, strcon); datap.Fill(dset); GridView1.DataSource = dset; this.GridView1.DataKeyNames = new string[] { \ 38 太原工业学院毕业设计(论文) GridView1.DataBind(); strcon.Close(); } 在GridView控件的RowEditing事件中获取要编辑的行。代码如下: protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; dsdatabind(); } 在GridView控件的RowUpdating事件中获取修改后的内容并保存到数据库中。应用((TextBox)(row.Cells[0].Controls[0])).Text.ToString()语句可以获取修改后的内容,将GridView控件的编辑索引设置为-1,取消编辑状态。代码如下: protected void GridViewUpdateEventArgs e) { strcon.Open(); string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString(); GridViewRow row = this.GridView1.Rows[e.RowIndex]; string ingo = \tb_BBS ((TextBox)(row.Cells[0].Controls[0])).Text.ToString() set title='\ + GridView1_RowUpdating(object sender, + \+ ((TextBox)(row.Cells[1].Controls[0])).Text.ToString() + \where id=\ SqlCommand goodsin = new SqlCommand(ingo, strcon); goodsin.ExecuteNonQuery(); strcon.Close(); GridView1.EditIndex = -1; dsdatabind(); 6.5 退出系统模块设计 网站后台的功能是对网站的数据信息进行管理,网站后台管理员通过后台 39 太原工业学院毕业设计(论文) 对网站进行维护,工作完成后一定要退出系统,如果被其它人进入,后果将不堪设想,网站数据可能会被破坏,实现退出网站后台,只需要将在“退出后台”按钮的Click事件中编写将存有管理员信息的Session变量清空的代码,和将页面重定向到网站前台首面的代码。代码如下: protected void ImageButton5_Click(object sender, ImageClickEventArgs e) { Session[\ Response.Redirect(\ } 40

