2010-12-05 189 views
4

我正在使用Microsoft Visual Basic 2010作爲使用c#的asp.net站點。檢查用戶是否登錄

我正在使用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 

回答

2

您不能直接訪問txtTitle和txtComment。

這些必須通過LoginView控件訪問,因爲它們包含在它中。

您應該使用FindControl方法來查找這些控件: LoginView.FindControl(的StringID)

+0

感謝這麼快作出反應。我嘗試了以下內容: ta.Insert1(articleid,gUser.ToString(),LoginView.FindControl(txtTitle).Text,oginView.FindControl(txtComment).Text); 但這仍顯示相同的錯誤:S。 – daveredfern 2010-12-05 21:02:10