2012-07-28 67 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