如何在 If 语句中使用会话隐藏或显示 asp:content



在我的生产.aspx Web 表单中,我有以下代码:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Index.Master" AutoEventWireup="true" CodeBehind="Production.aspx.cs" Inherits="WebPortal.Production" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">  
        <h3>PRODUCTION SITE</h3>    
        <img src="" alt="Production Logo" height="350" width="350" />
    </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Height="187px" Width="235px">
        <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
    </asp:Login>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
</asp:Content>

在我的生产.aspx.cs Web 表单中,我有以下代码:

namespace WebPortal
{
    public partial class Production : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                var GetSession = Session["Counter"];
                if (Convert.ToInt32(GetSession) == 1)
                {
                    // Show Content 1
                }
                else if (Convert.ToInt32(GetSession) == 2)
                {
                    // Show Content 2
                }
                else
                {
                    // Show Content 3
                }
            }
            catch (Exception ex) 
            {
                // MessageBox.show(ex.message.tostring());
            }
        }     
    }
}

我如何尝试根据会话值隐藏内容,例如当用户尝试通过其帐户登录并根据其访问级别时,他会得到一个 1,该 1 触发 if 语句,然后根据其要显示的内容的值。.

div 放入每个内容中,并像 folowing 一样显示/隐藏div:

.ASPX:

 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">  
 <div runat="server" id="div1">
    <h3>PRODUCTION SITE</h3>    
    <img src="" alt="Production Logo" height="350" width="350" />
    </div>
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
 <div runat="server" id="div2">
 <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Height="187px" Width="235px">
    <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
   </asp:Login>
  </div>
  </asp:Content>
  <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
  <div runat="server" id="div3">
  <p>q3</p>
   </div>
   </asp:Content>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    div1.Visible = div2.Visible = div3.Visible = false;
    if (Session["Counter"] != null)
    {
        int GetSession = (int)Session["Counter"];
        if (GetSession == 1)
            div1.Visible = true;
        else if (GetSession == 2)
            div2.Visible = true;
        else
            div3.Visible = true;
    }
    else
        div3.Visible = true;
}
如上所述

,您可能最好对不同的访问级别使用不同的嵌套母版页,但如果您坚持使用现有内容,则可以在具有三个视图的母版上使用多视图控件,每个视图包含一个占位符。

将 MasterType 指令添加到内容页,以便可以引用母版页,然后根据需要设置多视图活动索引。

代码没有经过测试,只是一个粗略的想法 - 我会使用开关块而不是"if"自己。

       protected void Page_Load(object sender, EventArgs e)
        {    
MultiView mv = (MultiView)Master.FindControl("multiView1");
                var GetSession = Session["Counter"];
                if (Convert.ToInt32(GetSession) == 1)
                {
                    mv.ActiveViewIndex = 0
                }
                else if (Convert.ToInt32(GetSession) == 2)
                {
                    mv.ActiveViewIndex = 1
                }
                else
                {
                    mv.ActiveViewIndex = 2
                }
            }

最新更新