datalist中的事件处理

本文关键字:事件处理 datalist | 更新日期: 2023-09-27 18:19:08

我有一个显示帖子的数据,我添加了文本框和按钮来添加评论,但是当我测试网站并添加评论时,我发现评论正确地添加了帖子,但空白评论添加到其他帖子…?

谁知道我该怎么处理这个?下面是我的代码:
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'

datalist中的事件处理

问题是您对列表中的每个项目执行相同的代码,而不是对那些有注释的项目或当前选中的项目执行相同的代码。

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

下面是我重写循环的方法:
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();
   }
}

这将确保对任何帖子的任何评论都将被更新,但您可能需要更新逻辑以确保仅更新选定的帖子。

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