网站后台管理之数据列表绑定、删除和修改(17)
发布时间:2021-06-07
发布时间:2021-06-07
后台代码主要包括初始化数据事件、编辑事件、修改事件和取消事件: //初始化下拉框数据事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowIndex < 0) return;
BLL.ArticleType articleTypeBll = new BLL.ArticleType(); DataSet ds = new DataSet(); ds = articleTypeBll.GetList("");
HiddenField hd = (HiddenField)e.Row.FindControl("HiddenFieldId");//获取主键值 BLL.Article articleBll = new BLL.Article(); Model.Article articleModel = new Model.Article();
articleModel = articleBll.GetModel(Convert.ToInt32(hd.Value)); //行的状态是: 编辑状态 或者 (交替行且是编辑状态) if(e.Row.RowState == DataControlRowState.Edit ||
e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)) {
DropDownList ddlType = e.Row.FindControl("ddlType") as DropDownList; if (ddlType != null) {
ddlType.DataValueField = "articleTypeId"; ddlType.DataTextField = "articleTypeName"; ddlType.DataSource = ds; ddlType.DataBind();
ddlType.Items.FindByValue(articleModel.articleTypeId.ToString()).Selected = true; } } }
//编辑事件
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
GridView1.EditIndex = e.NewEditIndex; BindGridView(); }
//修改事件
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
GridViewRow myrow = GridView1.Rows[e.RowIndex];
HiddenField hd = (HiddenField)myrow.Cells[0].FindControl("HiddenFieldId");//获取主键值
TextBox txtTitle = (TextBox)myrow.Cells[2].FindControl("txtTitle");//获取标题
DropDownList ddlType = myrow.Cells[3].FindControl("ddlType") as DropDownList;//获取下拉框值
上一篇:音频技术与应用发展趋势
下一篇:小学科学课外活动记录表