如何从非静态类ASP.Net c#设置界面可见性

时间:2018-01-18 11:31:42

标签: c# asp.net

我有一个班级来保存一周中不同日子的界面。我将表格单元格传递给该类并将其应用于列表。但是当我遍历我的类并调用我的HideInterface方法时,它并没有设置界面的可见性。我错过了什么?在我的例子中,我预计不会在星期二看到。我确信这与使属性保持静态有关,但我无法解决它。

private static List<DayOfWeek> days = new List<DayOfWeek>();

private static List<WhatsMyDay> myDays = new List<WhatsMyDay>();

  public class WhatsMyDay
  {
    public WhatsMyDay(DayOfWeek day, DropDownList am, 
                        DropDownList pm, HtmlTableCell dTitle)
    {
        Day = day;
        ddlAM = am;
        ddlPM = pm;
        tdTitle = dTitle;
    }

    public DayOfWeek Day { get; private set; }
    private static DropDownList ddlAM { get; set; }
    private static DropDownList ddlPM { get; set; }
    private static HtmlTableCell tdTitle { get; set; }

    public int? AM
    {
        get { 
            return Convert.ToInt32(ddlAM.SelectedValue) == -1 
                ? null 
                : (int?)Convert.ToInt32(ddlAM.SelectedValue); 
        }
    }

    public int? PM
    {
        get { 
            return Convert.ToInt32(ddlPM.SelectedValue) == -1 
                    ? null 
                    : (int?)Convert.ToInt32(ddlPM.SelectedValue); 
        }
    }

    public void ShowHideInterface(bool visiblity)
    {
        tdTitle.Visible = visiblity;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    days.Add(DayOfWeek.Monday);
    myDays.Add(new WhatsMyDay(
            DayOfWeek.Monday, ddlMondayAM, ddlMondayPM, tdMondayTitle));

    myDays.Add(new WhatsMyDay(
            DayOfWeek.Tuesday, ddlTuesdayAM, ddlTuesdayPM, tdTuesdayTitle));

    foreach (WhatsMyDay dayo in myDays)
    {
        dayo.ShowHideInterface(days.Contains(dayo.Day));
    }
}

1 个答案:

答案 0 :(得分:1)

如果okhttp是一个枚举(在那里没有显示),行为将被描述为:

由于您只在DayOfWeek变量中添加Monday,因此只有days可见,因为只有这种缩小的表达式为真:Monday

使[DayOfWeek.Monday].Contains(DayOfWeek.Monday)的{​​{1}}仅在循环的迭代中为真。

如果没有,那么visiblity是什么?

对于静态道具,您应该将它们更改为非静态道具,因为静态将使它们被ShowHideInterface的所有实例共享,而这似乎根本不是您的目标。