第一次加载控件的问题.
环境: Ajax .页面左是树. 右边是动态加载的控件.
解决方案:
树的 SelectedNodeChanged 在连续点两次之后, 会失去作用. 两种解决方法:
1.在 SelectedNodeChanged 事件中,把树添加一个不可见项,把该项设置成选中状态. 这样牺牲了 选中项 的样式.
2.在 Page_Load 中 BindTree() ; 因为是无刷新, 所以这个方法还算可以.
private void LoadControl()
{
this.tvDocMenu.SelectedNodeStyle.ForeColor = Color.Red;
string strSelValue = this.tvDocMenu.SelectedValue;
if (Enum.IsDefined(typeof(DocFlowState), strSelValue) == false) return;
DocFlowState selValue = (DocFlowState)Enum.Parse(typeof(DocFlowState), strSelValue);
switch (selValue)
{
case DocFlowState.Editing://正在起草
EditDocList con = (EditDocList)Page.LoadControl("EditDocList.ascx");
this.panControl.Controls.Add(con);
break;
case DocFlowState.UnDeal://待办
UnDealDocList unDeal = (UnDealDocList)Page.LoadControl("UnDealDocList.ascx");
this.panControl.Controls.Add(unDeal);
break;
}
}
{
this.tvDocMenu.SelectedNodeStyle.ForeColor = Color.Red;
string strSelValue = this.tvDocMenu.SelectedValue;
if (Enum.IsDefined(typeof(DocFlowState), strSelValue) == false) return;
DocFlowState selValue = (DocFlowState)Enum.Parse(typeof(DocFlowState), strSelValue);
switch (selValue)
{
case DocFlowState.Editing://正在起草
EditDocList con = (EditDocList)Page.LoadControl("EditDocList.ascx");
this.panControl.Controls.Add(con);
break;
case DocFlowState.UnDeal://待办
UnDealDocList unDeal = (UnDealDocList)Page.LoadControl("UnDealDocList.ascx");
this.panControl.Controls.Add(unDeal);
break;
}
}
在加载的时候, 就会遇到一个问题: 该控件是否是第一次加载. 明显的不能用 Page.IsPostBack 了. 在UserControl 里都 有一个IsPostBack属性.
public GridView TheGridView
{
get
{
if (this.QueryUI1 == null)
{
if (ViewState["TempTheGridView"] == null)
{
ViewState["TempTheGridView"] = new GridView();
}
return (GridView)ViewState["TempTheGridView"];
}
return this.QueryUI1.TheGridView;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsFirstLoad == true)
{
Bind(this.QueryUI1.BeginTime, this.QueryUI1.EndTime, this.QueryUI1.Title);
IsFirstLoad = false;
}
}
{
get
{
if (this.QueryUI1 == null)
{
if (ViewState["TempTheGridView"] == null)
{
ViewState["TempTheGridView"] = new GridView();
}
return (GridView)ViewState["TempTheGridView"];
}
return this.QueryUI1.TheGridView;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsFirstLoad == true)
{
Bind(this.QueryUI1.BeginTime, this.QueryUI1.EndTime, this.QueryUI1.Title);
IsFirstLoad = false;
}
}
调试发现: 在控件上还是好使.控件第一次加载 IsFirstLoad 是true,点击控件里的 Button 的话, IsFirstLoad 就变成 false 了.
但是在点击左边的树的时候, 该控件的 IsFirstLoad 还是false 导致无法绑定数据.
我们要在点击树的时候,手动的改变 IsFirstLoad ,就可以了.
EditDocList con = (EditDocList)Page.LoadControl("EditDocList.ascx");
this.panControl.Controls.Add(con);
con.IsFirstLoad = true ;
this.panControl.Controls.Add(con);
con.IsFirstLoad = true ;
作者:NewSea 出处:http://newsea.cnblogs.com/
QQ,MSN:iamnewsea@hotmail.com 如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。 |