2012-01-01 152 views
1

我正在创建一个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(); 
} 
+0

我可以看到一些ASPX代码吗? – dotnetstep 2012-01-01 07:06:12

回答

2

使用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); 
}