ASP.NET控制前台的数据项的显示和隐藏,主要通过反射机制来实现
如果要是一个一个判断,并通过Panel的Visible属性设置的话,就得写25次,难道没有别的办法了吗?当然有,就用反射机制就可以。
protected void Page_PreRender(object sender, EventArgs e)
{
PropertyInfo[] pi = this.Page.GetType().GetProperties();
foreach (PropertyInfo prop in pi)
{
if (prop != null && prop.PropertyType == typeof(string))
{
string val = (string)prop.GetValue(this, null);
if (string.IsNullOrEmpty(val))
{
string pnName = "pn_" + prop.Name;
if (Page.FindControl(pnName) != null)
{
Page.FindControl(pnName).Visible = false;
}
}
}
}
}