Вопрос

i am adding multiple checkboxes in my asp.net page by doing this:

 public static CheckBox[] chck;

on pageload:

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      con.Open();
        SqlCommand cmd = new SqlCommand("select count(CompanyName) from Stock_Company");
        cmd.Connection = con;
        comno = Convert.ToInt32(cmd.ExecuteScalar());
         con.Close();

        chck = new CheckBox[comno];
    }
 }

now i have a function which is generating the checkboxes :

   public void generatecheckbox1()
{

    con.Open();
    SqlCommand cmd = new SqlCommand("select CompanyName from Stock_Company");
    cmd.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataTable dt = ds.Tables[0];
    con.Close();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        chck[i] = new CheckBox();
        chck[i].ID = "chck" + Convert.ToString(i);
        chck[i].Text = dt.Rows[i]["CompanyName"].ToString();
        pnlcom1.Controls.Add(chck[i]);
        pnlcom1.Controls.Add(new LiteralControl("<br />"));
    }
  }

and i am calling this on a combobox event:

 protected void ddluserwebser_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddluserwebser.SelectedItem.Text == "Custom")
    {
        generatecheckbox1();
    }
 }

as far as this all are working fine ... but in a button click i want to get the select checkbox's text which i am not getting

i made a function :

   public string getbsecompany()
{
    string companyname = "";
    string bsetricker = "";
    con.Open();
    SqlCommand cmd = new SqlCommand("select CompanyName from Stock_Company");
    cmd.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataTable dt = ds.Tables[0];
    con.Close();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (chck[i].Checked == true)     **THE PROBLEM IS HERE**
        {
            companyname = chck[i].Text;
            con.Open();
            SqlCommand cmdd = new SqlCommand("select BSETickerCode from Stock_Company where CompanyName='" + companyname + "'");
            cmdd.Connection = con;
            bsetricker += bsetricker + "+" + cmdd.ExecuteScalar();
            con.Close();
        }
    }

    return bsetricker;
}

and i am calling it here:

  protected void btnusersave_Click(object sender, EventArgs e)
{
    string bsetricker = "";
    bsetricker = getbsecompany();
}

the problem is i am not getting the checked box's text. when i am checking if (chck[i].Checked == true) i am gettin false and all checkboxes are checked. What should i do now? any help

Это было полезно?

Решение

The dynamic controls should added to page in On_Init() for each time if you want it display in page. Else there's nothing you can get. Plus, better not use a static value to contains checkBox List, it will cause problem when multi user access same page. You can save them in session or try this.Form.FindControls()

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top