当前位置: 代码迷 >> ASP.NET >> 怎样控制用户控件的显示状态解决方法
  详细解决方案

怎样控制用户控件的显示状态解决方法

热度:8029   发布时间:2013-02-25 00:00:00.0
怎样控制用户控件的显示状态
有两个用户自定义的控件,A和B,现在我用载入页面以后,在页面初始化的时候,判断某个直是否等于我设置的直,如果是则显示控件A,隐藏控件B;否则,就隐藏控件A,显示控件B;
各位高手...请帮帮我这个小菜鸟吧~谢谢!

------解决方案--------------------------------------------------------
private void Page_Load(object sender,
System.EventArgs e)
{
string p = Request.QueryString.Get( "p ");
PlaceHolder1.Controls.Clear();
if (p == null)
PlaceHolder1.Controls.Add(LoadControl
( "modules/ " + "Default " + ".ascx "));
else
{
PlaceHolder1.Controls.Add(LoadControl
( "modules/ " + p + ".ascx "));
}
}


------解决方案--------------------------------------------------------
// 初学者,最好不要采取 LoadControl 方式,问题会接二连三的来
// 假如你,喜欢,最好先阅读
创建动态数据输入用户界面
http://www.microsoft.com/china/msdn/library/webservices/asp.net/dnasppDynamicUI.mspx?mfr=true


// 两个用户控件都拽到页面上,控制他们的 Visible 属性

protected void Page_Load(object sender, System.EventArgs e)
{
bool MyCondition;
// ....
MyUserControl_A.Visible = MyCondition;
MyUserControl_B.Visible = !MyCondition;

}
------解决方案--------------------------------------------------------
if (Session[ "userName "] == null || Session[ "userName "].ToString() == " ")
{
ASP.control_login_ascx login = (ASP.control_login_ascx)LoadControl( "~/control/login.ascx ");
PlaceHolder1.Controls.Add(login);
}
else
{
PlaceHolder1.Controls.Clear();
ASP.control_welcome_ascx welcome = (ASP.control_welcome_ascx)LoadControl( "~/control/welcome.ascx ");
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings[ "CXDBConnectionString "].ConnectionString);
sqlCon.Open();
string sentence = "select * from Users where UserID = ' " + Session[ "userName "].ToString() + " ' ";
SqlCommand sqlCom = new SqlCommand(sentence, sqlCon);
SqlDataReader sqlDr = sqlCom.ExecuteReader();
while (sqlDr.Read())
{
welcome.name = sqlDr.GetString(1);
welcome.money = sqlDr.GetDouble(11).ToString();
welcome.integral = sqlDr.GetInt32(3).ToString();
}
sqlDr.Close();
sqlCon.Close();
PlaceHolder1.Controls.Add(welcome);
}
  相关解决方案