ASP.NET visible=true不;t显示按钮

本文关键字:显示 按钮 true NET visible ASP | 更新日期: 2023-09-27 18:28:47

我将按钮设置为可见时遇到问题。我在MasterPage中创建了一些按钮,并将它们的可见性设置为false。(按钮.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
    }
}

ASP.NET visible=true不;t显示按钮

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

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: .....
        //...........
    }
}
 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;
    }
}