2011-12-28 52 views
0

我有一个datalist显示帖子,我添加了用于添加评论的文本框和按钮,但是当我测试网站并添加评论时,我发现该评论已正确添加,但空白评论添加到其他职位......?datalist中的事件处理

任何人都知道我该如何处理?这里是我的代码:

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' 
+0

你可以发布你的'评论'存储过程吗? – 2011-12-28 18:36:58

+0

当然,我会添加过程,但编辑处于挂起状态,我不知道为什么:D – 2011-12-28 18:44:14

+0

好吧,我现在看到更新存储过程,而且看起来还可以(除非用户永远不会看到打印语句) 。代码/存储过程如何检索评论? – 2011-12-28 19:27:44

回答

2

的问题是,你在列表中执行相同的代码为每个项目,而不是不得不离开他们的意见的项目或当前选择的项目。

解决此问题的一个办法是在执行数据库代码之前查看是否设置了文本。

这里是我会怎样改写循环:

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(); 
    } 
} 

这将确保在任何岗位任何评论将被更新,但您可能需要更新的逻辑,以确保只有选中的帖子被更新。

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

+0

非常感谢,很棒的主意,很好的工作 – 2011-12-28 22:06:22

+0

对不起,另一个问题,我怎样才能使评论直接添加后,而不刷新页面? – 2011-12-28 22:25:15