Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a table in my Database and according to number of record in table the checkbox created.

Example:- i have 5 record in table so i want 5 checkbox created automatically.

the previous code that i send you is written in the pageload event.

the problem is in the checkbox that are created everytime when pageload event fire.

I also want to use that checkbox ID in the checkbox event.

Please help if you know the answer.
Posted
Comments
Tom Marvolo Riddle 4-Apr-14 5:58am    
post your code
ZurdoDev 4-Apr-14 6:29am    
Dynamic controls should be added in the Page_Init event otherwise you may have problems with viewstate.
ritav1991 5-Apr-14 6:30am    
how to display question and answer option type wise.for example option type if multipal choice(single answer),multipal choice (multipal answer),image list control,date.
if select option type so display question and answer in page so how to create.

i think u should use checkboxlist .............

it bind your record and generate number of checkboxes...


for example

C#
protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
        {
            using (SqlConnection con=new SqlConnection ())
            {
con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
                con.Open ();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * from [Table]";
                    cmd.Connection = con;
                    using (DataSet ds = new DataSet())
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                            CheckBoxList1.DataSource = ds;
                            CheckBoxList1.DataTextField = "name";
                            CheckBoxList1.DataValueField = "Employeeid";
                            CheckBoxList1.DataBind();
                        }
                    }
                }



or try this link

http://www.aspsnippets.com/Articles/Bind-CheckBoxList-from-Database-in-ASPNet.aspx[^]
 
Share this answer
 
Comments
ritav1991 5-Apr-14 6:29am    
how to display question and answer option type wise.for example option type if multipal choice(single answer),multipal choice (multipal answer),image list control,date.
if select option type so display question and answer in page so how to create.
Hi,

You need to write the code in a condition that will restrict

C#
public void Page_Load(object sender, EventArgs e)
{
  if(!this.IsPostBack)
  {
     using (SqlConnection con=new SqlConnection ())
            {
              //your connection string
                con.Open ();
                using (SqlCommand cmd = new SqlCommand())
                {
                   // your sql command
                    using (SQLDataReader rd = cmd.ExecuteReader())
                    {
                        while(rd.Read())
                        {
                            // suppose you have a panel in your page as panel1
                            CheckBox chk = new CheckBox();
                            chk.ID=string.Fromat("CheckBox_{0}", rd["PrimaryKey/AnyValue"]);
                           // bind event to checkbox
                            chk.Checked+= new EventHandler(checkbox_checked);
                             
                        }
                    }
                }
             } 
  }
}

public void checkbox_checked(object sender, EventArgs e)
{
    //your code----------------
}
 
Share this answer
 
Comments
ritav1991 5-Apr-14 6:30am    
how to display question and answer option type wise.for example option type if multipal choice(single answer),multipal choice (multipal answer),image list control,date.
if select option type so display question and answer in page so how to create.
You also need to add this check box in panel

so here is the code.


C#
while(rd.Read())
                        {
                            // suppose you have a panel in your page as panel1
                            CheckBox chk = new CheckBox();
                            chk.ID=string.Fromat("CheckBox_{0}", rd["PrimaryKey/AnyValue"]);
                           // bind event to checkbox
                            chk.Checked+= new EventHandler(checkbox_checked);
                            panel1.Controls.Add(chk);    
                        }
 
Share this answer
 
Comments
ritav1991 5-Apr-14 6:31am    
how to display question and answer option type wise.for example option type if multipal choice(single answer),multipal choice (multipal answer),image list control,date.
if select option type so display question and answer in page so how to create.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900