1.在Gridview_RowDataBound中根据条件创建控件。
2.测试时发现在回发的时候动态创建的控件没有了。
解决:在回发的时候再动态创建控件
Code
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
CreateGridControl();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
CreateGridControl();
}
3.使用过程中发现,绑定二次绑定数据源时,控件没值了
原因:Gridview_RowDataBound绑定时,给控件的赋值都进行了IsPostBack的判断.所以没值
解决:提供绑定时的属性支持
bool ReRowDataBoundFlag
{
get
{
if (ViewState["RowDataBoundFlag"] == null)
{
ViewState["RowDataBoundFlag"] = true;
}
return (bool)ViewState["RowDataBoundFlag"];
}
set
{
ViewState["RowDataBoundFlag"] = value;
}
}
{
get
{
if (ViewState["RowDataBoundFlag"] == null)
{
ViewState["RowDataBoundFlag"] = true;
}
return (bool)ViewState["RowDataBoundFlag"];
}
set
{
ViewState["RowDataBoundFlag"] = value;
}
}
总结:
1.动态绑定的控件的值已经被写入了ViewState了,只要在回发的时候创建控件,就可以正常处理了。
2.动态绑定的控件如果有数据源(例如:DrowDownList),那么数据源需要重新绑定。
3.也许ReRowDataBoundFlag属性可以不要,原因时在DataBind中进行的IsPostBack的判断也许是多余的。(没有经过测试)
4.如果不用的行要根据不同的值创建不同的对象,最好将值用过HiddenField进行保存,这样在创建控件的时候就可以有判断的依据
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using BLL.PersonnelAction;
using BLL.Sys;
using BLL.RightCenter;
using BLL.EmployeeNS;
using BLL.Model;
using BLL.PersonnelAction.PersonnelActionbusinessClass;
public partial class UC : System.Web.UI.UserControl
{
public List<PersonnelActionItemBindEntity> GetEntities()
{
List<PersonnelActionItemBindEntity> list = new List<PersonnelActionItemBindEntity>();
foreach (GridViewRow row in gvActionTerm.Rows)
{
var hd = (HiddenField)row.FindControl("hfActionItemId");
int? actionItemId = hd.Value.Trim().Length == 0 ? null : (int?)int.Parse(hd.Value);
hd = (HiddenField)row.FindControl("hfActionTermId");
int actionTermId = int.Parse(hd.Value);
string oldData = ((HiddenField)row.FindControl("hdOldData")).Value;
DropDownList ddl = row.FindControl("ddlReasonId") as DropDownList;
int? reasonId = ddl.SelectedValue == Utility.DllShowDefaultValue ? null : (int?)int.Parse(ddl.SelectedValue);
string newData = null;
var tb = row.FindControl("tbNewData") as TextBox;
if (tb != null)
{
newData = tb.Text;
}
ddl = row.FindControl("ddl_position") as DropDownList;
if (ddl != null)
{
newData = ddl.SelectedValue;
}
ddl = row.FindControl("ddlNewData") as DropDownList;
if (ddl != null)
{
newData = ddl.SelectedValue;
}
list.Add(new PersonnelActionItemBindEntity(actionTermId, actionItemId, oldData, newData, reasonId));
}
return list;
}
public void BindEntities(List<PersonnelActionItemBindEntity> list)
{
ReRowDataBoundFlag = true;
gvActionTerm.DataSource = list;
gvActionTerm.DataBind();
}
bool ReRowDataBoundFlag
{
get
{
if (ViewState["RowDataBoundFlag"] == null)
{
ViewState["RowDataBoundFlag"] = true;
}
return (bool)ViewState["RowDataBoundFlag"];
}
set
{
ViewState["RowDataBoundFlag"] = value;
}
}
gvActionTerm_RowDataBound#region gvActionTerm_RowDataBound
protected void gvActionTerm_RowDataBound(object sender, GridViewRowEventArgs e)
{
//创建建各种控件,以及绑定信息
if (e.Row.RowType == DataControlRowType.DataRow)
{
var entity = e.Row.DataItem as PersonnelActionItemBindEntity;
//属性名称
string temp_PropertyName;
string temp_TermName;
if (IsPostBack && !ReRowDataBoundFlag)
{
temp_PropertyName = ((HiddenField)e.Row.FindControl("hfPropertyName")).Value;
temp_TermName = ((Label)e.Row.FindControl("lbTermName")).Text;
}
else
{
var temp_pat = PAT.GetPersonnerActionTerm(entity.ActionTermId);
temp_PropertyName = temp_pat.PropertyName;
temp_TermName = temp_pat.Name;
}
if (!IsPostBack || ReRowDataBoundFlag)
{
//One Column
((HiddenField)e.Row.FindControl("hfActionItemId")).Value = (entity.ActionItemId == null ? "" : entity.ActionItemId.ToString());
((HiddenField)e.Row.FindControl("hfActionTermId")).Value = entity.ActionTermId.ToString();
((HiddenField)e.Row.FindControl("hfPropertyName")).Value = temp_PropertyName;
((Label)e.Row.FindControl("lbTermName")).Text = temp_TermName;
//Two Column
string str_two = "";
switch (temp_PropertyName)
{
case PersonnelActionItemConst.PersonnelActionSalary:
str_two = entity.OldData;
break;
case PersonnelActionItemConst.PersonnelActionGrade:
case PersonnelActionItemConst.PersonnelActionPosition:
case PersonnelActionItemConst.PersonnelActionSalaryGrade:
case PersonnelActionItemConst.PersonnelActionCategory:
str_two = PersonnelActionBLL.GetNameByNo(temp_PropertyName, entity.OldData);
break;
default:
break;
}
((HiddenField)e.Row.FindControl("hdOldData")).Value = entity.OldData;
((Label)e.Row.FindControl("lbOldData")).Text = str_two;
}
//Threee Column
switch (temp_PropertyName)
{
case PersonnelActionItemConst.PersonnelActionSalary:
var tb = new TextBox();
tb.ID = "tbNewData";
tb.CssClass = "ShortText";
if (!IsPostBack || ReRowDataBoundFlag)
tb.Text = entity.NewData;
e.Row.Cells[2].Controls.Add(tb);
break;
case PersonnelActionItemConst.PersonnelActionPosition:
DropDownList ddl_position = new DropDownList();
ddl_position.ID = "ddl_position";
if (!IsPostBack || ReRowDataBoundFlag)
{
PersonnelActionBLL.BindDropDownListByType(ddl_position, temp_PropertyName, entity.NewData, GetPageLoginInfo());
ddl_position.SelectedValue = entity.NewData;
}
DropDownList ddl_depart = new DropDownList();
ddl_depart.ID = "ddl_depart";
if (!IsPostBack || ReRowDataBoundFlag)
{
Utility.InitDDLDepartment(ddl_depart, GetPageLoginInfo());
if (!string.IsNullOrEmpty(entity.NewData))
{
var departobj = (new Position()).GetPositionInfo(Convert.ToInt32(entity.NewData));
if (departobj != null)
{
int departid = departobj.DepartmentID;
ddl_depart.SelectedValue = departid.ToString();
}
}
}
ddl_depart.Width = 100;
ddl_position.Width = 100;
ddl_depart.AutoPostBack = true;
ddl_depart.SelectedIndexChanged += new EventHandler(ddl_depart_SelectedIndexChanged);
e.Row.Cells[2].Controls.Add(ddl_depart);
e.Row.Cells[2].Controls.AddAt(1, ddl_position);
break;
case PersonnelActionItemConst.PersonnelActionGrade:
case PersonnelActionItemConst.PersonnelActionSalaryGrade:
case PersonnelActionItemConst.PersonnelActionCategory:
DropDownList ddl = new DropDownList();
ddl.ID = "ddlNewData";
if (!IsPostBack || ReRowDataBoundFlag)
{
PersonnelActionBLL.BindDropDownListByType(ddl, temp_PropertyName, entity.NewData, GetPageLoginInfo());
ddl.SelectedValue = entity.NewData;
}
e.Row.Cells[2].Controls.Add(ddl);
break;
default:
break;
}
//Four Column
DropDownList actionReason = e.Row.FindControl("ddlReasonId") as DropDownList;
if (!IsPostBack || ReRowDataBoundFlag)
{
Utility.InitActionReason(actionReason, entity.ActionTermId);
actionReason.SelectedValue = entity.ReasonId.HasValue ? entity.ReasonId.ToString() : "";
}
}
}
#endregion
ddl_depart_SelectedIndexChanged#region ddl_depart_SelectedIndexChanged
void ddl_depart_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl_depart = (DropDownList)sender;
int departmentId = int.Parse(ddl_depart.SelectedValue);
var ddl_position = ddl_depart.Parent.Parent.FindControl("ddl_position") as DropDownList;
Utility.InitPosition(departmentId, ddl_position);
}
#endregion
PersonnelActionTerm pAT;
PersonnelActionTerm PAT
{
get
{
if (pAT == null)
pAT = new PersonnelActionTerm();
return pAT;
}
}
public LoginInfo GetPageLoginInfo()
{
return ((BasePage)this.Page).loginInfo;
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
CreateGridControl();
}
CreateGridControl#region CreateGridControl
/**//// <summary>
/// 回发时创建Grid变动列控件
/// </summary>
void CreateGridControl()
{
//创建建各种控件,以及绑定信息
foreach (GridViewRow row in gvActionTerm.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//属性名称
string temp_PropertyName;
string temp_TermName;
temp_PropertyName = ((HiddenField)row.FindControl("hfPropertyName")).Value;
temp_TermName = ((Label)row.FindControl("lbTermName")).Text;
//Threee Column
switch (temp_PropertyName)
{
case PersonnelActionItemConst.PersonnelActionSalary:
var tb = new TextBox();
tb.ID = "tbNewData";
tb.CssClass = "ShortText";
row.Cells[2].Controls.Add(tb);
break;
case PersonnelActionItemConst.PersonnelActionPosition:
DropDownList ddl_position = new DropDownList();
ddl_position.ID = "ddl_position";
DropDownList ddl_depart = new DropDownList();
ddl_depart.ID = "ddl_depart";
ddl_depart.Width = 100;
ddl_position.Width = 100;
ddl_depart.AutoPostBack = true;
Utility.InitDDLDepartment(ddl_depart, GetPageLoginInfo());
ddl_depart.SelectedIndexChanged += new EventHandler(ddl_depart_SelectedIndexChanged);
row.Cells[2].Controls.Add(ddl_depart);
row.Cells[2].Controls.AddAt(1, ddl_position);
break;
case PersonnelActionItemConst.PersonnelActionGrade:
case PersonnelActionItemConst.PersonnelActionSalaryGrade:
case PersonnelActionItemConst.PersonnelActionCategory:
DropDownList ddl = new DropDownList();
ddl.ID = "ddlNewData";
PersonnelActionBLL.BindDropDownListByType(ddl, temp_PropertyName, "", GetPageLoginInfo());
row.Cells[2].Controls.Add(ddl);
break;
default:
break;
}
}
}
#endregion
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using BLL.PersonnelAction;
using BLL.Sys;
using BLL.RightCenter;
using BLL.EmployeeNS;
using BLL.Model;
using BLL.PersonnelAction.PersonnelActionbusinessClass;
public partial class UC : System.Web.UI.UserControl
{
public List<PersonnelActionItemBindEntity> GetEntities()
{
List<PersonnelActionItemBindEntity> list = new List<PersonnelActionItemBindEntity>();
foreach (GridViewRow row in gvActionTerm.Rows)
{
var hd = (HiddenField)row.FindControl("hfActionItemId");
int? actionItemId = hd.Value.Trim().Length == 0 ? null : (int?)int.Parse(hd.Value);
hd = (HiddenField)row.FindControl("hfActionTermId");
int actionTermId = int.Parse(hd.Value);
string oldData = ((HiddenField)row.FindControl("hdOldData")).Value;
DropDownList ddl = row.FindControl("ddlReasonId") as DropDownList;
int? reasonId = ddl.SelectedValue == Utility.DllShowDefaultValue ? null : (int?)int.Parse(ddl.SelectedValue);
string newData = null;
var tb = row.FindControl("tbNewData") as TextBox;
if (tb != null)
{
newData = tb.Text;
}
ddl = row.FindControl("ddl_position") as DropDownList;
if (ddl != null)
{
newData = ddl.SelectedValue;
}
ddl = row.FindControl("ddlNewData") as DropDownList;
if (ddl != null)
{
newData = ddl.SelectedValue;
}
list.Add(new PersonnelActionItemBindEntity(actionTermId, actionItemId, oldData, newData, reasonId));
}
return list;
}
public void BindEntities(List<PersonnelActionItemBindEntity> list)
{
ReRowDataBoundFlag = true;
gvActionTerm.DataSource = list;
gvActionTerm.DataBind();
}
bool ReRowDataBoundFlag
{
get
{
if (ViewState["RowDataBoundFlag"] == null)
{
ViewState["RowDataBoundFlag"] = true;
}
return (bool)ViewState["RowDataBoundFlag"];
}
set
{
ViewState["RowDataBoundFlag"] = value;
}
}
gvActionTerm_RowDataBound#region gvActionTerm_RowDataBound
protected void gvActionTerm_RowDataBound(object sender, GridViewRowEventArgs e)
{
//创建建各种控件,以及绑定信息
if (e.Row.RowType == DataControlRowType.DataRow)
{
var entity = e.Row.DataItem as PersonnelActionItemBindEntity;
//属性名称
string temp_PropertyName;
string temp_TermName;
if (IsPostBack && !ReRowDataBoundFlag)
{
temp_PropertyName = ((HiddenField)e.Row.FindControl("hfPropertyName")).Value;
temp_TermName = ((Label)e.Row.FindControl("lbTermName")).Text;
}
else
{
var temp_pat = PAT.GetPersonnerActionTerm(entity.ActionTermId);
temp_PropertyName = temp_pat.PropertyName;
temp_TermName = temp_pat.Name;
}
if (!IsPostBack || ReRowDataBoundFlag)
{
//One Column
((HiddenField)e.Row.FindControl("hfActionItemId")).Value = (entity.ActionItemId == null ? "" : entity.ActionItemId.ToString());
((HiddenField)e.Row.FindControl("hfActionTermId")).Value = entity.ActionTermId.ToString();
((HiddenField)e.Row.FindControl("hfPropertyName")).Value = temp_PropertyName;
((Label)e.Row.FindControl("lbTermName")).Text = temp_TermName;
//Two Column
string str_two = "";
switch (temp_PropertyName)
{
case PersonnelActionItemConst.PersonnelActionSalary:
str_two = entity.OldData;
break;
case PersonnelActionItemConst.PersonnelActionGrade:
case PersonnelActionItemConst.PersonnelActionPosition:
case PersonnelActionItemConst.PersonnelActionSalaryGrade:
case PersonnelActionItemConst.PersonnelActionCategory:
str_two = PersonnelActionBLL.GetNameByNo(temp_PropertyName, entity.OldData);
break;
default:
break;
}
((HiddenField)e.Row.FindControl("hdOldData")).Value = entity.OldData;
((Label)e.Row.FindControl("lbOldData")).Text = str_two;
}
//Threee Column
switch (temp_PropertyName)
{
case PersonnelActionItemConst.PersonnelActionSalary:
var tb = new TextBox();
tb.ID = "tbNewData";
tb.CssClass = "ShortText";
if (!IsPostBack || ReRowDataBoundFlag)
tb.Text = entity.NewData;
e.Row.Cells[2].Controls.Add(tb);
break;
case PersonnelActionItemConst.PersonnelActionPosition:
DropDownList ddl_position = new DropDownList();
ddl_position.ID = "ddl_position";
if (!IsPostBack || ReRowDataBoundFlag)
{
PersonnelActionBLL.BindDropDownListByType(ddl_position, temp_PropertyName, entity.NewData, GetPageLoginInfo());
ddl_position.SelectedValue = entity.NewData;
}
DropDownList ddl_depart = new DropDownList();
ddl_depart.ID = "ddl_depart";
if (!IsPostBack || ReRowDataBoundFlag)
{
Utility.InitDDLDepartment(ddl_depart, GetPageLoginInfo());
if (!string.IsNullOrEmpty(entity.NewData))
{
var departobj = (new Position()).GetPositionInfo(Convert.ToInt32(entity.NewData));
if (departobj != null)
{
int departid = departobj.DepartmentID;
ddl_depart.SelectedValue = departid.ToString();
}
}
}
ddl_depart.Width = 100;
ddl_position.Width = 100;
ddl_depart.AutoPostBack = true;
ddl_depart.SelectedIndexChanged += new EventHandler(ddl_depart_SelectedIndexChanged);
e.Row.Cells[2].Controls.Add(ddl_depart);
e.Row.Cells[2].Controls.AddAt(1, ddl_position);
break;
case PersonnelActionItemConst.PersonnelActionGrade:
case PersonnelActionItemConst.PersonnelActionSalaryGrade:
case PersonnelActionItemConst.PersonnelActionCategory:
DropDownList ddl = new DropDownList();
ddl.ID = "ddlNewData";
if (!IsPostBack || ReRowDataBoundFlag)
{
PersonnelActionBLL.BindDropDownListByType(ddl, temp_PropertyName, entity.NewData, GetPageLoginInfo());
ddl.SelectedValue = entity.NewData;
}
e.Row.Cells[2].Controls.Add(ddl);
break;
default:
break;
}
//Four Column
DropDownList actionReason = e.Row.FindControl("ddlReasonId") as DropDownList;
if (!IsPostBack || ReRowDataBoundFlag)
{
Utility.InitActionReason(actionReason, entity.ActionTermId);
actionReason.SelectedValue = entity.ReasonId.HasValue ? entity.ReasonId.ToString() : "";
}
}
}
#endregion
ddl_depart_SelectedIndexChanged#region ddl_depart_SelectedIndexChanged
void ddl_depart_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl_depart = (DropDownList)sender;
int departmentId = int.Parse(ddl_depart.SelectedValue);
var ddl_position = ddl_depart.Parent.Parent.FindControl("ddl_position") as DropDownList;
Utility.InitPosition(departmentId, ddl_position);
}
#endregion
PersonnelActionTerm pAT;
PersonnelActionTerm PAT
{
get
{
if (pAT == null)
pAT = new PersonnelActionTerm();
return pAT;
}
}
public LoginInfo GetPageLoginInfo()
{
return ((BasePage)this.Page).loginInfo;
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
CreateGridControl();
}
CreateGridControl#region CreateGridControl
/**//// <summary>
/// 回发时创建Grid变动列控件
/// </summary>
void CreateGridControl()
{
//创建建各种控件,以及绑定信息
foreach (GridViewRow row in gvActionTerm.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//属性名称
string temp_PropertyName;
string temp_TermName;
temp_PropertyName = ((HiddenField)row.FindControl("hfPropertyName")).Value;
temp_TermName = ((Label)row.FindControl("lbTermName")).Text;
//Threee Column
switch (temp_PropertyName)
{
case PersonnelActionItemConst.PersonnelActionSalary:
var tb = new TextBox();
tb.ID = "tbNewData";
tb.CssClass = "ShortText";
row.Cells[2].Controls.Add(tb);
break;
case PersonnelActionItemConst.PersonnelActionPosition:
DropDownList ddl_position = new DropDownList();
ddl_position.ID = "ddl_position";
DropDownList ddl_depart = new DropDownList();
ddl_depart.ID = "ddl_depart";
ddl_depart.Width = 100;
ddl_position.Width = 100;
ddl_depart.AutoPostBack = true;
Utility.InitDDLDepartment(ddl_depart, GetPageLoginInfo());
ddl_depart.SelectedIndexChanged += new EventHandler(ddl_depart_SelectedIndexChanged);
row.Cells[2].Controls.Add(ddl_depart);
row.Cells[2].Controls.AddAt(1, ddl_position);
break;
case PersonnelActionItemConst.PersonnelActionGrade:
case PersonnelActionItemConst.PersonnelActionSalaryGrade:
case PersonnelActionItemConst.PersonnelActionCategory:
DropDownList ddl = new DropDownList();
ddl.ID = "ddlNewData";
PersonnelActionBLL.BindDropDownListByType(ddl, temp_PropertyName, "", GetPageLoginInfo());
row.Cells[2].Controls.Add(ddl);
break;
default:
break;
}
}
}
#endregion
}
}