在事件处理DataList控件 [英] Event handling in datalist

查看:107
本文介绍了在事件处理DataList控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有展示职位DataList和我添加文本框和按钮添加评论,但是当我测试的网站,并添加评论我发现注释为后加正确,但一个空白注释添加到休息的帖子...?

任何人都知道我可以处理这个问题?这里是我的code:

 保护无效Button9_Click1(对象发件人,EventArgs的发送)
{
    的foreach(在DataList2.Items的DataListItem项)
    {
        文本框TextBox1中=(文本框)item.FindControl(TextBox1中);
        字符串文本= TextBox1.Text;
        标签post_IDLabel =(标签)item.FindControl(post_IDLabel);
        字符串connStr = ConfigurationManager.ConnectionStrings [MyDbConn]的ToString()。
        康涅狄格州的SqlConnection =新的SqlConnection(connStr);
        CMD的SqlCommand =新的SqlCommand(注释,康恩);
        cmd.CommandType = CommandType.StoredProcedure;
        INT POST_ID = Convert.ToInt32(post_IDLabel.Text);
        字符串评论=文本;
        字符串email =会话[电子邮件]的ToString()。
        INT COURSE_ID = Convert.ToInt32(的Request.QueryString [courseID]);
        cmd.Parameters.Add(新的SqlParameter(@ COURSE_ID,COURSE_ID));
        cmd.Parameters.Add(新的SqlParameter(@评论,评论));
        cmd.Parameters.Add(新的SqlParameter(@ myemail,电子邮件));
        cmd.Parameters.Add(新的SqlParameter(@帖子ID,POST_ID));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

这是注释程序

  CREATE PROC评论
@comment VARCHAR(100),
 @myemail VARCHAR(30),
 @postID INT,
 @course_ID INT
 如
 IF @myemail IN(SELECT student_email FROM Students_Subscribes_Course_pages WHERE订阅= 1)
 OR @myemail IN(SELECT added_email FROM Lecturers_Adds_Academics_Course_page WHERE COURSE_ID = @course_ID)
 和@postID IN(SELECT POST_ID来自帖子WHERE @postID = @postID)OR @myemail =
 (SELECT page_creator FROM Course_pages WHERE COURSE_ID = @course_ID)和@postID IN
 (SELECT POST_ID FROM帖子)
 INSERT INTO Members_Comments_Posts(评议,POST_ID,COM​​MENT_CONTENT)
 VALUES(@myemail,@postID,@comment)
 其他
PRINT对不起,你没有订阅或交找不到


解决方案

的问题是,你执行相同的code列表中的每一个项目,而不是那些有留给他们的意见或当前所选项目的项目

一个解决方法是,看文本执行数据库code之前设置。

下面是我会怎样改写循环:

 的foreach(在DataList2.Items的DataListItem项)
{
    文本框TextBox1中=(文本框)item.FindControl(TextBox1中);
    字符串文本= TextBox1.Text;
    如果(!string.IsNullOrEmpty(文本)){
      标签post_IDLabel =(标签)item.FindControl(post_IDLabel);
      字符串connStr = ConfigurationManager.ConnectionStrings [MyDbConn]的ToString()。
      康涅狄格州的SqlConnection =新的SqlConnection(connStr);
      CMD的SqlCommand =新的SqlCommand(注释,康恩);
      cmd.CommandType = CommandType.StoredProcedure;
      INT POST_ID = Convert.ToInt32(post_IDLabel.Text);
      字符串评论=文本;
      字符串email =会话[电子邮件]的ToString()。
      INT COURSE_ID = Convert.ToInt32(的Request.QueryString [courseID]);
      cmd.Parameters.Add(新的SqlParameter(@ COURSE_ID,COURSE_ID));
      cmd.Parameters.Add(新的SqlParameter(@评论,评论));
      cmd.Parameters.Add(新的SqlParameter(@ myemail,电子邮件));
      cmd.Parameters.Add(新的SqlParameter(@帖子ID,POST_ID));
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();
   }
}

这将确保在任何岗位任何意见将被更新,但您可能需要更新逻辑,以确保只有选定的职位将被更新。

此外,你应该将你的连接开/关和命令生成圈外,以便更有效。

I am having a datalist showing posts and I added textbox and button for adding a comment, but when I test the website and add a comment I find that the comment is added for the post correctly but a blank comment added to the rest of the posts...?

Anyone know how i can handle this? Here is my code:

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

and this is the comment procedure

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'

解决方案

The problem is that you execute the same code for every item in the list rather than those items that had comments left for them or the currently selected item.

One solution to this is to see if the text is set before executing the database code.

Here is how I would rewrite the loop:

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

This will ensure that any comments on any posts will be updated, but you may need to update the logic to ensure that only the selected post is updated.

Also, you should move your connection open/close and command creation out of the loop in order to be more efficient.

这篇关于在事件处理DataList控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆