Check if user is logged in
I am using Microsoft visual basic 2010 for a asp.net site using c#.
I am using the asp.net configuration for user registration. 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.
I have the following in 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:开发者_如何学JAVATextBox 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:
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. 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.
These must be accessed through LoginView control since they are contained in it.
You should use FindControl method to locate these controls: LoginView.FindControl( stringId)
精彩评论