简体   繁体   English

检查用户是否登录

[英]Check if user is logged in

I am using Microsoft visual basic 2010 for a asp.net site using c#. 我正在使用c#将Microsoft Visual Basic 2010用于asp.net网站。

I am using the asp.net configuration for user registration. 我正在使用asp.net配置进行用户注册。 I have a comments form which I want to appear only if a user is logged in. 我有一个评论表单,仅当用户登录后才想显示。

I now there is a toolbox helper thing called Login View which does exactly what I want but as soon as I put a form inside the code won't compile because it cannot find the textbox fields. 现在,我有一个名为Login View的工具箱帮助程序,它完全可以实现我想要的功能,但是一旦我在代码中放入一个表单,它便无法编译,因为它找不到文本框字段。

I have the following in NewsArticle.aspx: 我在NewsArticle.aspx中具有以下内容:

<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
      <div class="postcomment">
           <p><a href="/account/Login.aspx">Login</a> or <a href="/account/Register.aspx">register</a> to post a comment.</p>
      </div>
 </AnonymousTemplate>
 <LoggedInTemplate>
      <div class="formcomment">
          <asp:TextBox ID="txtTitle" textMode="SingleLine" runat="server"></asp:TextBox>
          <asp:TextBox ID="txtComment" TextMode="MultiLine" runat="server"></asp:TextBox>
          <asp:Button ID="cmdUpdate" runat="server" Text="Add Comment" onclick="cmdUpdate_Click" />
      </div>
</LoggedInTemplate>

On the NewsArticle.aspx.cs I have: 在NewsArticle.aspx.cs上,我有:

protected void cmdUpdate_Click(object sender, EventArgs e) {

    // Get user id
    Guid gUser;
    MembershipUser user = Membership.GetUser(Page.User.Identity.Name);
    gUser = (Guid)user.ProviderUserKey;

    // get article id
    int articleid = Convert.ToInt16(Request.QueryString["id"]);

    // Add to db
    FrontendTableAdapters.NewsCommentTableAdapter ta = new FrontendTableAdapters.NewsCommentTableAdapter();
    ta.Insert1(articleid, gUser.ToString(), txtTitle.Text, txtComment.Text);

    // Redirect back to article
    Response.Redirect(String.Format("NewsArticle.aspx?id={0}#comments", articleid));
}

If I take the form out of asp:LoginView it works fine. 如果我采用asp:LoginView的形式,则效果很好。 Inside I get the following: 在内部,我得到以下信息:

Error 2 The name 'txtTitle' does not exist in the current context NewsArticle.aspx.cs 59 53 Figmentville
Error 3 The name 'txtComment' does not exist in the current context \NewsArticle.aspx.cs 59 68 Figmentville

You cannot directly access txtTitle and txtComment. 您不能直接访问txtTitle和txtComment。

These must be accessed through LoginView control since they are contained in it. 由于它们包含在其中,因此必须通过LoginView控件进行访问。

You should use FindControl method to locate these controls: LoginView.FindControl( stringId) 您应该使用FindControl方法找到以下控件:LoginView.FindControl(stringId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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