2012-01-01 154 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); 
}