ASP.NET - how to insert a record into database

Asked By aman on 05-Jan-12 04:50 AM
hello everyone
i have a screen which have multiple dynamic checkbox and two textbox.
here is the sample code of adding dynamically checkbox.

CheckBox obj = new CheckBox();
Repeater1.Controls.Add(obj);
obj.Text = objDataRow[0].ToString();

according to the user selected checkbox, i want to insert the record in my database table but i cannot find the check box value
so how can i find the checkbox field which i have created dynamically.

thanks
regards
Riley K replied to aman on 05-Jan-12 04:55 AM


You have to create a custom event handler for checkbox checkedchanged event

CheckBox chkDynamic;
 
chkDynamic = new CheckBox();
    chkDynamic.ID = "chkExample";
    chkDynamic.Text = "Check / Uncheck";
    chkDynamic.AutoPostBack = true;
    chkDynamic.CheckedChanged += new EventHandler
               (chkDynamic_CheckedChanged);
    this.Form.Controls.Add(chkDynamic);
the code for checkbox checked

protected void chkDynamic_CheckedChanged
        (object sender, EventArgs e)
   {
     if (chkDynamic.Checked)
      //write code to insert in database
     else if (!chkDynamic.Checked)
       lblMessage.Text = "checkbox is not checked";
   }


Regards
Jitendra Faye replied to aman on 05-Jan-12 04:59 AM
Try this code-

for (int i = 0; i <= Repeater1.Items.Count - 1; i++)
        {
          CheckBox chk = (CheckBox )Repeater1.Items[i].FindControl("checkboxname");
       if(chk!=null)
        {
           //Insert code
        } 
        }