2012-07-28 66 views
0

如何將相應的評論刪除到使用ListView發佈特定評論的用戶?我已經添加了一個按鈕,並使其Visible爲false,這樣在我的.cs代碼中,當我檢查當前登錄用戶是發佈該評論的用戶時,刪除按鈕將會顯示。目前,我想這:相應地刪除評論,以確定當前用戶是否是在ListView中發佈評論的用戶

protected void PostCommentButton_Click(object sender, EventArgs e) 
{ 
    if (!Page.IsValid) 
     return; 


    MembershipUser currentUser = Membership.GetUser(); 
    Guid currentUserId = (Guid)currentUser.ProviderUserKey; 


    string connectionString = ConfigurationManager.ConnectionStrings["CommentConnectionString"].ConnectionString; 
    string insertSql = "INSERT INTO Comments(Subject, Body, UserId) VALUES(@Subject, @Body, @UserId)"; 

    using (SqlConnection myConnection = new SqlConnection(connectionString)) 
    { 
     myConnection.Open(); 

     SqlCommand myCommand = new SqlCommand(insertSql, myConnection); 
     myCommand.Parameters.AddWithValue("@Subject", Subject.Text.Trim()); 
     myCommand.Parameters.AddWithValue("@Body", Body.Text.Trim()); 
     myCommand.Parameters.AddWithValue("@UserId", currentUserId); 

     myCommand.ExecuteNonQuery(); 

     myConnection.Close(); 

     if (currentUser.UserName == Eval("UserName").ToString()) 
    { 
     Control deleteButton = e.Item.FindControl("Button2"); 
     deleteButton.Visible = true; 
    } 

}

但是,這是給我的錯誤。它聲明'System.EventArgs'不包含'Item'的定義,並且沒有找到接受'System.EventArgs'類型的第一個參數的擴展方法'Item'。我將這段代碼放在我的PostComment按鈕中。我在任何地方做錯了嗎?

這是顯示的錯誤。

編譯錯誤

說明:該請求提供服務所需資源的編譯過程中出現錯誤。請查看以下具體的錯誤細節並適當修改您的源代碼。

編譯器錯誤消息:CS1061:'System.EventArgs'沒有包含'Item'的定義,也沒有擴展方法可以找到'Item'接受類型'System.EventArgs'的第一個參數(你是否缺少使用指令或程序集引用)

源錯誤:

Line 59:   TextBox2.Text = string.Empty; 
Line 60: 
Line 61:   if (e.Item.ItemType == ListViewItemType.DataItem) 
Line 62:   { 
Line 63:    ListViewDataItem currentItem = (ListViewDataItem)e.Item; 
+0

你不能使用e.Item.FindControl ................. – 2012-07-28 15:04:20

+0

發佈完整的代碼... – 2012-07-28 15:11:21

+0

那麼我使用哪種方法?通常當我鍵入Button2.Visible = true;不會有任何錯誤,但我不知道爲什麼在這種情況下,如果我要使用此方法,則會出現錯誤。因此,我改變了上述方式,但我也得到了錯誤。 – kelly 2012-07-28 15:13:28

回答

0

如果將Button2所在的頁面,那麼你可以找到參考的

Button btn = Page.FindControl("Button2"); 

如果它位於列表視圖內,那麼你可以找到它

if (e.Item.ItemType == ListViewItemType.DataItem) 
{ 
    ListViewDataItem currentItem = (ListViewDataItem)e.Item; 
    Button btn = (Button)currentItem.FindControl("Button2"); 
} 
+0

我在我的帖子評論按鈕單擊中輸入代碼後出現此錯誤。我必須把你建議的代碼放在別的地方嗎? – kelly 2012-07-28 18:00:15

+0

你有什麼錯誤? – 2012-07-28 18:14:36

+1

只是把這個代碼'ListViewDataItem currentItem =(ListViewDataItem)e.Item; Button btn =(Button)currentItem.FindControl(「Button2」);'不需要將if條件 – 2012-07-28 18:15:12