2011-12-28 51 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