简体   繁体   中英

Masterpage in ASP.net

I am using a masterpage and have created a very simple website. I need to hide and unhide access to some links and buttons using c#. the code works fine in the home link but for some reason I cant get it to work on other links of the webpage Here is part of the code from the working line that works fine from the link HOME

 <td class="style5"><a class="Link2" id="ad" runat="server" href="admin.aspx" style="color: #FFFFFF">Admin</a>
            </td>
        <td class="style5"><a class="Link1" href="login.aspx" style="color: #FFFFFF" id="ab" runat="server">Login</a>

         <asp:Button id="cd" runat="server" Text="Logout" onclick="Button1_Click" 
                Width="100%" BackColor="Red" CausesValidation="False" Font-Bold="True" 
                ForeColor="White" />

            </td>

the c#

namespace Aptech_Project
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if ((string)Session["us1"] == "admin")
            {
                ab.Disabled = true;
                ab.Visible = false;
                cd.Visible = true;
                ad.Disabled = false;
                ad.Visible = true;
            }
            else

            {

                ab.Disabled = false;
                ab.Visible = true;              
                cd.Visible = false;               
                ad.Disabled = true;
                ad.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["us1"] = null;
            Response.Redirect("home.aspx");
        }
    }
}

I have the exact same code on a Page called about us but it does not work on the About us page.Here is the code that is not working

<td class="style5"><a class="Link2" href="admin.aspx" style="color: #FFFFFF" id="ad" runat="server">Admin</a>
            </td>

       <td class="style5"><a class="Link1" href="login.aspx" style="color: #FFFFFF" id="ab" runat="server">Login</a>
         <asp:Button id="cd" runat="server" Text="Logout" onclick="Button1_Click" 
                Width="100%" BackColor="Red" CausesValidation="False" Font-Bold="True" 
                ForeColor="White" />
            </td>

the c#

namespace Aptech_Project
{
    public partial class aboutus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


                if ((string)Session["us1"] == "admin")
                {
                    ab.Disabled = true;
                    ab.Visible = false;
                    cd.Visible = true;
                    ad.Disabled = false;
                    ad.Visible = true;
                }
                else
                {

                ab.Disabled = false;
                ab.Visible = true;              
                cd.Visible = false;               
                ad.Disabled = true;
                ad.Visible = false;
                }
            }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["us1"] = null;
            Response.Redirect("home.aspx");
        }
    }
}

Not sure how the page load function works with Master Pages. Does the page load of the master page work first and then the content pages or vise versa.

Please help.. Thanks in advance.

如果您要从“主页”转到“关于我们”,则可能是因为您将Session变量设置为null。

尝试使用Page.Master.FindControl("controlID")在母版页中找到控件

Instead of using FindControl i recommended to use MASTERTYPE directive to utilize Master page's properties and their controls properties directly. By following declaration of code in content page you can access properties.

<%@ MasterType VirtualPath="~/Site.master" %>

Then in content page you just have to use MASTER keyword to utilize any properties.

Like Master.lblControl1.Text="Hi";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM