C# 如何在If语句中使用会话隐藏或显示Asp:内容
在我的Production.aspx Web表单中,我有以下代码:C# 如何在If语句中使用会话隐藏或显示Asp:内容,c#,asp.net,.net,session,C#,Asp.net,.net,Session,在我的Production.aspx Web表单中,我有以下代码: <%@ Page Title="" Language="C#" MasterPageFile="~/Index.Master" AutoEventWireup="true" CodeBehind="Production.aspx.cs" Inherits="WebPortal.Production" %> <asp:Content ID="Content1" ContentPlaceHolder
<%@ 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>
如何根据会话值隐藏内容,例如当用户尝试通过其帐户登录时,根据其访问级别,他会得到触发if语句的1,然后根据其值显示什么内容。将div放入每个内容中,并显示/隐藏div,如下所示: ASPX:
正如上面所建议的,对于不同的访问级别,您可能最好使用不同的嵌套母版页,但是如果您仍然无法使用现有的内容,您可以在母版上使用一个多视图控件,其中有三个视图,每个视图都有一个占位符 向内容页添加MasterType指令,以便可以引用母版页,然后根据需要设置多视图活动索引 代码没有经过测试,只是一个粗略的想法——我会使用开关块而不是自己的“如果”
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
}
}
IMHO您需要浏览不同角色的母版页。
<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;
}
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
}
}