求教aspnet高手。无法保存状态的问题。
protected void Page_Load(object sender, EventArgs e)
{
if (Session[ "oldascx "] == null)
{
this.Panel1.Controls.Clear();
}
else
{
UserControl u = (UserControl)LoadControl( "ascx/ " + Session[ "oldascx "].ToString());
this.Panel1.Controls.Clear();
this.Panel1.Controls.Add(u);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
UserControl u = (UserControl)LoadControl( "ascx/ " + "a.ascx ");
this.Panel1.Controls.Clear();
this.Panel1.Controls.Add(u);
Session[ "oldascx "] = "a.ascx ";
}
protected void Button2_Click(object sender, EventArgs e)
{
UserControl u = (UserControl)LoadControl( "ascx/ " + "b.ascx ");
this.Panel1.Controls.Clear();
this.Panel1.Controls.Add(u);
Session[ "oldascx "] = "b.ascx ";
}
实现动态加载ascx控件。控件中是一些按钮和gridview。b.ascx中有按钮叫Process
问题如下:
1 当首次进入的时候,点击按钮Button2载入b.ascx控件时Process按钮正常。没有问题。
2 当首次进入后,点击按钮Button2 显示b.ascx在点击按钮Button1显示a.ascx 在点击按钮button2显示b.ascx时(就是来回倒几次),Process按钮必须点击2次才能触发事件。
不知为何?已经弄了一天了。也没弄好。
------解决方案--------------------try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session[ "oldascx "] == null)
{
this.Panel1.Controls.Clear();
}
else
{
UserControl u = (UserControl)LoadControl( "ascx/ " + Session[ "oldascx "].ToString());
this.Panel1.Controls.Clear();
this.Panel1.Controls.Add(u);
}
}
}
.......
------解决方案--------------------我看了一下,是我疏忽了,我少了一句话:
if (ViewState[ "oldascx "] != null)
{
UserControl u = (UserControl)LoadControl( "ascx/ " + (string)ViewState[ "oldascx "]);