2012-01-01 6 views
1

データリストに表示された各投稿の評価尺度の中にRadioButtonListを含むデータリストを作成していますが、1つの投稿を評価すると、他のすべての投稿は同じと評価されます。 、ありがとう。 PS:私は、問題は、私はそれを削除した場合、私はDataListItemを得るために、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

答えて

2

使用NamingContainerにアクセスすることはできません誰foreachループであることを知っています。

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); 
} 
関連する問題