In this code I dynamically create checkboxes which I populate with data from my database.
My intention is when I press the btnProba button to show the text property only from the selected checkboxes. But it gives me an error in this row saying that index is out of range! I can’t explain why.
lblProba.Text = myche[0];
public partial class FormEGN : System.Web.UI.Page
{
string mynewstring;
List<string> myche = new List<string>();
CheckBoxList mycheckbox = new CheckBoxList();
protected void Page_Load(object sender, EventArgs e)
{
mynewstring = (string)Session["id2"];
// lblProba.Text = mynewstring;
if(!IsPostBack)
{
ddlNumberTourists.Items.Add("1");
ddlNumberTourists.Items.Add("2");
ddlNumberTourists.Items.Add("3");
}
}
protected void ddlNumberTourists_SelectedIndexChanged(object sender, EventArgs e)
{
int numTourists = Convert.ToInt32(ddlNumberTourists.SelectedItem.Text);
for (int i = 0; i < numTourists; i++)
{
Label myLabel = new Label();
myLabel.ID = "lblAccomodation" + (i + 1).ToString();
myLabel.Text = "Настаняване Турист" + (i + 1).ToString();
Page.FindControl("form1").Controls.Add(myLabel);
DropDownList myDropDownList = new DropDownList();
myDropDownList.ID = "ddlTourist" + i.ToString();
Page.FindControl("form1").Controls.Add(myDropDownList);
Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));
string connectionString = "Server=localhost\\SQLEXPRESS;Database=EXCURSIONSDATABASE;Trusted_Connection=true";
string query =
"SELECT Extra_Charge_ID, Excursion_ID, Amout, Extra_Charge_Description FROM EXTRA_CHARGES WHERE Excursion_ID=" + mynewstring;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(query, conn);
try
{
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
int s = 0;
while (rd.Read())
{
// CheckBox myCheckbox = new CheckBox();
// myCheckbox.ID = "ckbExtraCharge" + i.ToString() + s.ToString();
// myCheckbox.Text = rd["Extra_Charge_Description"].ToString();
// Page.FindControl("form1").Controls.Add(myCheckbox);
// Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));
// s++;
mycheckbox.ID = "chkblextracharge" + i.ToString() + s.ToString();
mycheckbox.Items.Add(rd["Extra_Charge_Description"].ToString());
Page.FindControl("form1").Controls.Add(mycheckbox);
if (mycheckbox.Items[s].Selected == true)
{
myche.Add(mycheckbox.Items[s].Text);
}
s++;
}
}
catch (Exception ex)
{ }
}
}
protected void btnProba_Click(object sender, EventArgs e)
{
lblProba.Text = myche[0];
}
protected void btnReserve_Click(object sender, EventArgs e)
{
string num = Request.QueryString["ExcursionID"];
Response.Redirect(String.Format("ClintsInformation.aspx?Excursiondate_ID={0}",num));
}
}
}
The function
ddlNumberTourists_SelectedIndexChangedis the one that set themychewhen is find some selected, but the data is lost on the second call where is theThe two calls are happening on different post back.
You need to call the
ddlNumberTourists_SelectedIndexChanged(object sender, EventArgs e)on button click and not on every change of the dropdownlist, eg I rename it toCheckWhatIsSelected()and here is the code:One other possible solution is to save the
mycheon viewstate.