我正在使用Microsoft Visual Basic 2010开发ASP.NET网站,使用C#语言。
我正在使用ASP.NET配置进行用户注册。我有一个评论表单,只想在用户登录后显示。
我知道有一个名为Login View的工具箱助手可以实现我想要的功能,但是一旦我放置一个表单,代码就无法编译,因为它找不到文本框字段。
在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>
在 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));
}
如果我将表单从asp:LoginView中取出,它可以正常工作。在里面我会得到以下内容:
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