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();
}
Use
NamingContainerto obtain DataListItem.