C# datalist中的事件处理

C# datalist中的事件处理,c#,asp.net,visual-studio,visual-studio-2010,C#,Asp.net,Visual Studio,Visual Studio 2010,我有一个显示帖子的数据列表,我添加了文本框和添加评论的按钮,但当我测试网站并添加评论时,我发现该评论是正确添加的,但在其余帖子中添加了一条空白评论 有人知道我怎么处理吗?这是我的密码: protected void Button9_Click1(object sender, EventArgs e) { foreach (DataListItem item in DataList2.Items) { TextBox TextBox1 = (TextBox)item

我有一个显示帖子的数据列表,我添加了文本框和添加评论的按钮,但当我测试网站并添加评论时,我发现该评论是正确添加的,但在其余帖子中添加了一条空白评论

有人知道我怎么处理吗?这是我的密码:

protected void Button9_Click1(object sender, EventArgs e)
{
    foreach (DataListItem item in DataList2.Items)
    {
        TextBox TextBox1 = (TextBox)item.FindControl("TextBox1");
        string text = TextBox1.Text;
        Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("comment", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        int post_ID = Convert.ToInt32(post_IDLabel.Text);
        string comment = text;
        string email = Session["email"].ToString();
        int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
        cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
        cmd.Parameters.Add(new SqlParameter("@comment", comment));
        cmd.Parameters.Add(new SqlParameter("@myemail", email));
        cmd.Parameters.Add(new SqlParameter("@postID", post_ID));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }       
} 
这是评论程序

CREATE PROC comment
@comment VARCHAR (100),
 @myemail VARCHAR (30),
 @postID INT,
 @course_ID INT
 AS
 IF @myemail IN (SELECT student_email FROM Students_Subscribes_Course_pages WHERE subscribed = 1) 
 OR @myemail IN (SELECT added_email FROM Lecturers_Adds_Academics_Course_page WHERE course_ID = @course_ID) 
 AND @postID IN (SELECT post_ID FROM Posts WHERE @postID = @postID) OR @myemail = 
 (SELECT page_creator FROM Course_pages WHERE course_ID = @course_ID) AND @postID IN 
 (SELECT post_ID FROM Posts)
 INSERT INTO Members_Comments_Posts (commenter,post_ID, comment_content)
 VALUES (@myemail, @postID, @comment)
 ELSE 
PRINT 'sorry, you are not subscribed or post not found'

问题在于,您对列表中的每个项目执行相同的代码,而不是对那些为其留下注释的项目或当前选定的项目执行相同的代码

一种解决方案是在执行数据库代码之前查看文本是否已设置

下面是我将如何重写循环:

foreach (DataListItem item in DataList2.Items)
{
    TextBox TextBox1 = (TextBox)item.FindControl("TextBox1");
    string text = TextBox1.Text;
    if (!string.IsNullOrEmpty(text)) {
      Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
      string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
      SqlConnection conn = new SqlConnection(connStr);
      SqlCommand cmd = new SqlCommand("comment", conn);
      cmd.CommandType = CommandType.StoredProcedure;
      int post_ID = Convert.ToInt32(post_IDLabel.Text);
      string comment = text;
      string email = Session["email"].ToString();
      int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
      cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
      cmd.Parameters.Add(new SqlParameter("@comment", comment));
      cmd.Parameters.Add(new SqlParameter("@myemail", email));
      cmd.Parameters.Add(new SqlParameter("@postID", post_ID));
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();
   }
}
这将确保更新任何帖子上的任何评论,但您可能需要更新逻辑,以确保仅更新所选帖子


此外,为了提高效率,您应该将连接打开/关闭和命令创建移出循环。

您可以发布
注释
存储过程吗?当然,我会添加该过程,但编辑仍在等待中,我不知道为什么:好的,我现在看到更新存储过程,这看起来也不错(除非用户永远看不到打印语句)。检索注释的代码/存储过程如何?通过从comments表中选择comment.post\u ID=control(post\u IDLabel)从数据列表中检索注释我现在看到问题了。我已经用解决方案更新了答案。对不起,另一个问题,如何在添加后直接显示评论而不刷新页面?