访问数据列表内的控件

发布于 2024-12-23 10:16:02 字数 1365 浏览 2 评论 0原文

我正在创建一个数据列表,其中包含 RadioButtonList 作为数据列表中显示的每个帖子的评级量表,但是当我对一篇帖子进行评级时,所有其他帖子的评级相同,您能告诉我问题出在哪里吗,谢谢。 PS:我知道问题出在 foreach 循环中,如果我删除它,我将无法访问 RadioButtonList 或 postIDLabel

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) {
  foreach (DataListItem item in DataList2.Items) {
    RadioButtonList RadioButtonList1=(RadioButtonList)item.FindControl("RadioButtonList1");
    string choice = RadioButtonList1.SelectedValue;

    Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
    int post_ID = Convert.ToInt32(post_IDLabel.Text);
    int value = Convert.ToInt32(choice);

    string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
    SqlConnection conn = new SqlConnection(connStr);

    SqlCommand cmd = new SqlCommand("rate", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    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("@postID", post_ID));
    cmd.Parameters.Add(new SqlParameter("@myemail", email));
    cmd.Parameters.Add(new SqlParameter("@rate", value));

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

    Response.Write(choice);
  }
  DataList2.DataBind();
}

i am creating a datalist with RadioButtonList inside as a rating scale for each post desplayed in the datalist, however when i rate one post, all the other posts rated the same, can you tell me where's the problem, thanks.
PS: i know that the problem is in foreach loop whoever if i removed it, i will not be able to access the RadioButtonList or postIDLabel

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) {
  foreach (DataListItem item in DataList2.Items) {
    RadioButtonList RadioButtonList1=(RadioButtonList)item.FindControl("RadioButtonList1");
    string choice = RadioButtonList1.SelectedValue;

    Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
    int post_ID = Convert.ToInt32(post_IDLabel.Text);
    int value = Convert.ToInt32(choice);

    string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
    SqlConnection conn = new SqlConnection(connStr);

    SqlCommand cmd = new SqlCommand("rate", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    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("@postID", post_ID));
    cmd.Parameters.Add(new SqlParameter("@myemail", email));
    cmd.Parameters.Add(new SqlParameter("@rate", value));

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

    Response.Write(choice);
  }
  DataList2.DataBind();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2024-12-30 10:16:02

使用NamingContainer获取DataListItem。

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
 RadioButtonList rad = sender as RadioButtonList;
 DataListItem item = rad.NamingContainer as DataListItem;
 Label lab = item.FindControl("postIDLabel") as Label ;
 Response.Write(lab.Text);
}

Use NamingContainer to obtain DataListItem.

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
 RadioButtonList rad = sender as RadioButtonList;
 DataListItem item = rad.NamingContainer as DataListItem;
 Label lab = item.FindControl("postIDLabel") as Label ;
 Response.Write(lab.Text);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文