ASP.NET visible = true不显示按钮

时间:2014-10-31 12:21:41

标签: c# asp.net

通过设置我的按钮可见,我遇到了问题。 我在MasterPage中创建了一些按钮,并将其可见性设置为false。 (button.Visible = false;) 按下按钮后,我被重定向到另一页。 在这个页面(来自MasterPage的一个孩子)我想设置一些按钮可见(Master.FindControl(“button”)。Visible = true),但这是我的问题。它没有出现。

MasterPage.master:

        <asp:Button ID="b_home" runat="server" Text="Home" CssClass="button" OnClick="b_home_Click"/>
        <asp:Button ID="b_profil" runat="server" Text="Profil" CssClass="button" OnClick="b_profil_Click"/>
        <asp:Button ID="b_reservieren" runat="server" Text="Reservieren" CssClass="button" OnClick="b_reservieren_Click"/>
        <asp:Button ID="b_verleihhistorie" runat="server" Text="Verleihhistorie" CssClass="button" OnClick="b_verleihhistorie_Click"/>
        <asp:Button ID="b_warenausgang" runat="server" Text="Warenausgang" CssClass="button" OnClick="b_warenausgang_Click"/>
        <asp:Button ID="b_wareneingang" runat="server" Text="Wareneingang" CssClass="button" OnClick="b_wareneingang_Click"/>
        <asp:Button ID="b_neueKunden" runat="server" Text="Neue Kunden" CssClass="button" OnClick="b_neueKunden_Click"/>
        <asp:Button ID="b_kontakte" runat="server" Text="Kontakte" CssClass="button" OnClick="b_kontakte_Click"/>
    </div>

Master.master.cs

protected void Page_Load(object sender, EventArgs e)
{
    b_home.Visible = true;
    b_kontakte.Visible = true;
    b_profil.Visible = false;
    b_reservieren.Visible = false;
    b_verleihhistorie.Visible = false;
    b_warenausgang.Visible = false;
    b_wareneingang.Visible = false;
    b_neueKunden.Visible = false;

}

Default.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
{


    if(MasterPage.istAngemeldet)
    {

       // .p = db.Persons.Where(s => s.Email==benutzername && s.Passwort== passwort).FirstOrDefault();


        l_willkommen.Text= "Herzlich Willkommen bei Bee-Coop.at, " + MasterPage.p.Vorname + "!";
        LinkButton l1 = (LinkButton)Master.FindControl("LinkButton1");
        l1.Text = "[Abmelden]";

        #region SideMenu_Control

        switch(MasterPage.hatRolle)
        {
            case 0: Master.FindControl("b_home").Visible = true;
                    Master.FindControl("b_kontakte").Visible = true;
                    Master.FindControl("b_profil").Visible = true;
                    Master.FindControl("b_reservieren").Visible = true;
                    Master.FindControl("b_verleihhistorie").Visible = true;
                    Master.FindControl("b_warenausgang").Visible = true;
                    Master.FindControl("b_wareneingang").Visible = true;
                    Master.FindControl("b_neueKunden").Visible = true;
                    break;

            case 1: Master.FindControl("b_home").Visible = true;
                    Master.FindControl("b_kontakte").Visible = true;
                    Master.FindControl("b_profil").Visible = true;
                    Master.FindControl("b_reservieren").Visible = true;
                    Master.FindControl("b_verleihhistorie").Visible = true;
                    Master.FindControl("b_warenausgang").Visible = true;
                    Master.FindControl("b_wareneingang").Visible = true;
                    break;

            case 2: Master.FindControl("b_home").Visible = true;
                    Master.FindControl("b_kontakte").Visible = true;
                    Master.FindControl("b_profil").Visible = true;
                    Master.FindControl("b_reservieren").Visible = true;
                    Master.FindControl("b_verleihhistorie").Visible = true;
                    break;

            case 3: Master.FindControl("b_home").Visible = true;
                    Master.FindControl("b_kontakte").Visible = true;
                    break;                
        }

        #endregion
    }
}

2 个答案:

答案 0 :(得分:1)

每次显示页面时都会启动page_load事件,因此您将控件的可见性设置为false,您需要将您的代码更改为:

protected void Page_Load(object sender, EventArgs e)
{
          switch(MasterPage.hatRolle)
    {
        case 0: b_home.Visible = true;
                b_kontakte.Visible = true;
                b_profil.Visible = true;
                b_reservieren.Visible = true;
                b_verleihhistorie.Visible = true;
                b_warenausgang.Visible = true;
                b_wareneingang.Visible = true;
                b_neueKunden.Visible = true;
                break;

        //case 1: .....
        //...........
    }
}

答案 1 :(得分:0)

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        b_home.Visible = true;
        b_kontakte.Visible = true;
        b_profil.Visible = false;
        b_reservieren.Visible = false;
        b_verleihhistorie.Visible = false;
        b_warenausgang.Visible = false;
        b_wareneingang.Visible = false;
        b_neueKunden.Visible = false;
    }
}