【问题标题】:Sessions & ContentPlaceHolders会话和 ContentPlaceHolders
【发布时间】:2013-04-02 18:39:59
【问题描述】:

是否可以在 MasterPage 中设置一个 Container(ContentPlaceHolder),并在需要时隐藏它?

例如,我想放置 2 个文本框和一个按钮供用户登录,但如果会话仍在运行(即用户已经登录),我希望将这 2 个文本框和按钮更改为欢迎消息(例如:“欢迎,用户名”)。

MasterPage(注意:并非所有内容都已编码)

<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">

    Protected Sub Button1_Click(sender As Object, e As EventArgs)

        Dim loginSQL As New SqlCommand
        Dim loginComm As String

        Dim CommonFunctions As New CommonFunctions()
        Dim dec_pass As String = CommonFunctions.EncryptPassword(txtLoginPass.Text.Trim)

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")


        loginComm = "SELECT userid FROM users WHERE username=@username and password=@password"

        conn.Open()


        loginSQL = New SqlCommand(loginComm, conn)
        loginSQL.Parameters.AddWithValue("@username", txtLoginUser.Text.ToString)
        loginSQL.Parameters.AddWithValue("@password", dec_pass)
        Dim dr As SqlDataReader = loginSQL.ExecuteReader()

        If dr.HasRows Then
            Session("userid") = dr("userid")
        ElseIf dr.HasRows = False Then
            lblRegister.ForeColor = Drawing.Color.Red
            lblRegister.Text = "Incorrect Username/Password."
        End If

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs)

        If Session("userid") <> "" Then
            txtLoginUser.Visible = False
            txtLoginPass.Visible = False
        Else

            Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")

            Dim useridComm As String = "SELECT name, surname FROM users WHERE userid=@userid"
            Dim sqlUserID As New SqlCommand

            conn.Open()

            Dim userid As String = Session("userid")

            sqlUserID = New SqlCommand(useridComm, conn)
            sqlUserID.Parameters.AddWithValue("@userid", userid)
            Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()

            lblLoggedIn.Text = "Welcome," + txtLoginUser.Text
        End If
    End Sub

</script>



<head>
    <meta charset="utf-8" />
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="style.css" type="text/css" media="screen, projection" />
</head>

<body>

    <form id="form1" runat="server">

<div id="wrapper">

    <header id="header">
        <strong>Header:</strong> Mobile CMS

        </header>


<section id="login">

    <div id="login-form">



            <asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server">

                <p>

                    <asp:Label ID="lblUsername" runat="server" Font-Bold="True" Text="U:"></asp:Label>
&nbsp;<asp:TextBox ID="txtLoginUser" runat="server" BorderStyle="None" BorderWidth="0px" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Label ID="lblUsername0" runat="server" Font-Bold="True" Text="P:"></asp:Label>
                    <asp:TextBox ID="txtLoginPass" runat="server" BorderStyle="None" BorderWidth="0px" TextMode="Password" Wrap="False"></asp:TextBox>
&nbsp;
                    <asp:Button ID="btnLogin" runat="server" BorderStyle="None" OnClick="Button1_Click" Text="Login" />

                </p>

                <p>

                    <asp:Label ID="lblRegister" runat="server" Font-Bold="True" Font-Underline="True" ForeColor="#0000CC" Text="Register"></asp:Label>

                    <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
                        <asp:Label ID="lblLoggedIn" runat="server" Text=""></asp:Label>
                    </asp:ContentPlaceHolder>

                </p>


                </asp:ContentPlaceHolder>



    </div>

</section>

<div class="navigation-bar">
       <ul class="navigation-menu">

            <li><a href="#" class="home">Home</a></li>
            <li><a href="#" class="mainsettings">Settings</a></li>
            <li><a href="#" class="profile">Profile</a>

                <ul>
                    <li><a href="#" class="messages">Messages</a></li>
                    <li><a href="#" class="settings">Profile Settings</a></li>
                </ul>

            </li>
            <li><a href="#" class="uploads">Uploads</a></li>
            <li><a href="#" class="documents">Media</a>


                <ul>
                    <li><a href="#" class="docs">Documents</a></li>
                    <li><a href="#" class="others">Others</a></li>
                </ul>

            </li>

            <li><a href="#" class="projects">Projects</a>


                <ul>
                    <li><a href="#" class="yprojects">Your Projects</a></li>
                    <li><a href="#" class="otherprojects">Other Projects</a></li>
                </ul>

            </li>

        </ul>

    </div>


    <section id="middle">

        <div id="container">
            <div id="content">
                <div>
                    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

                    </asp:ContentPlaceHolder>
                </div>
        </div>
        </div>



    </section>

    <footer id="footer">
        <strong>Footer:</strong> adsfdsgfds
    </footer>

</div>

    </form>

</body>
</html>

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 您将不得不发布包含控件的 html,以便我们查看页面的结构。
  • 您可以简单地使用 Visible="true|false" 根据用户是否为Authenticated。你在哪里卡住了?
  • @Win 是的,我读过一些关于内容占位符的内容,您可以这样做。所以最好的方法是制作 2 个内容占位符,其中 1 个可见,而另一个不可见,对吧?

标签: asp.net vb.net session contentplaceholder


【解决方案1】:

理想情况下,您希望使用 ASP.Net 的 LoginViewLoginStatus 控件。

由于您只有一页,因此只需使用两个Panels - 根据用户是否经过身份验证显示和隐藏。

注意:在这种情况下不要使用ContentPlaceHolder

更新:

如果您想尝试,这里是LoginView 的示例代码。这是一种更喜欢的方式。

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" 
    Inherits="WebApplication2010.WebForm6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate>
            <asp:Label ID="lblUsername" runat="server" Font-Bold="True" Text="U:"></asp:Label>
            &nbsp;<asp:TextBox ID="txtLoginUser" runat="server" BorderStyle="None" BorderWidth="0px"
                TextMode="Password" Wrap="False"></asp:TextBox>
            &nbsp;
            <asp:Label ID="lblUsername0" runat="server" Font-Bold="True" Text="P:"></asp:Label>
            <asp:TextBox ID="txtLoginPass" runat="server" BorderStyle="None" BorderWidth="0px"
                TextMode="Password" Wrap="False"></asp:TextBox>
            &nbsp;
            <asp:Button ID="btnLogin" runat="server" BorderStyle="None" OnClick="Button1_Click"
                Text="Login" />
        </AnonymousTemplate>
        <LoggedInTemplate>
            Welcome!
        </LoggedInTemplate>
    </asp:LoginView>
    </form>
</body>
</html>

protected void Button1_Click(object sender, EventArgs e)
{
    var txtLoginUser = LoginView1.FindControl("txtLoginUser") as TextBox;
    var txtLoginPass = LoginView1.FindControl("txtLoginPass") as TextBox;

    // Your authentication

    // If validation is successful, create Authentication ticket
    FormsAuthentication.SetAuthCookie(txtLoginUser.Text, false);
}

【讨论】:

    【解决方案2】:

    首先,您在另一个ContentPlaceHolder 中有一个ContentPlaceHolder,我看不出有什么原因。

    <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
       <asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server">
    

    对于手头的问题,我会根据用户是否登录来隐藏ContentPlaceHolder 中包含的控件,而不是隐藏占位符,但这取决于你。我还将userid 列添加到您的 SQL 查询中。

    以下内容应该可以帮助您入门,请注意这是我的未经测试

    SELECT userid, username, password FROM users ....

    If dr.HasRows Then
      Session("userid") = dr("userid")
    ElseIf dr.HasRows = False Then
       lblRegister.ForeColor = Drawing.Color.Red
       lblRegister.Text = "Incorrect Username/Password."
    End If
    

    现在你有了这个,你可以检查Page_Load上的Session变量,看看用户是否登录。

    Protected Sub Page_Load(sender As Object, e As EventArgs)
      If Session("userid") <> "" Then
        txtLoginUser.Visible = False
        txtLoginPass.Visible = False
      Else
        'Set your label text here, query the database to get the First and Second name of the user using the `userid`
      End If
    End Sub
    

    编辑 阅读完毕后需要关闭阅读器:

    dr.Close()datareader.Close()

    这行lblLoggedIn.Text = "Welcome," + txtLoginUser.Text 也需要:

    lblLoggedIn.Text = datareader("name").ToString() & " " &
    datareader("surname").ToString()
    

    【讨论】:

    • 嗨。我试图通过隐藏/显示 ContentPlaceHolders 来做到这一点,它给了我这个错误:The parameterized query '(@username nvarchar(7),@password nvarchar(4000))SELECT username,' expects the parameter '@password', which was not supplied. 在这条线上Dim dr As SqlDataReader = loginSQL.ExecuteReader()
    • 不需要对文本框中提供的值进行加密吗?而不是解密?
    • 照你说的做,我得到一个错误:The parameterized query '(@userid nvarchar(4000))SELECT name, surname FROM users WHERE us' expects the parameter '@userid', which was not supplied.我用新代码更新了问题。
    • 是的,在页面加载时。 Dim userid As String = Session("userid") sqlUserID = New SqlCommand(useridComm, conn) sqlUserID.Parameters.AddWithValue("@userid", userid) Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()
    • 您页面中的操作员加载方式错误。将&lt;&gt; 更改为=
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 2010-10-20
    • 2015-10-10
    • 1970-01-01
    • 2011-09-16
    • 2012-09-27
    相关资源
    最近更新 更多