event handling of dynamically created checkboxes in a dynamic table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isha610
    New Member
    • May 2013
    • 1

    event handling of dynamically created checkboxes in a dynamic table

    In my project, initially, i am submitting which year,division and subject (of students) i want to access in the student database. Once i click the submit button, dynamic checkboxes are created in a dynamic table according to the query submited above. However i am facing a problem while accessing/event handling these checkboxes to update the attendance of students for the above submited subject back into the database! please help!

    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    
    namespace iso_generator
    {
        public partial class attendance : System.Web.UI.Page
        {
    
            CheckBox cb1;
            System.Data.OleDb.OleDbConnection con;
            System.Data.OleDb.OleDbDataAdapter da;
            System.Data.DataSet ds;
    
            static int inc = -1;
            static int maxRows;
          
            
    
    
           protected void Page_Load(object sender, EventArgs e)
            {
                con = new System.Data.OleDb.OleDbConnection();
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ISO\\DB\\db1.mdb";
                con.Open();
    
    
                string division = ddlDivision.Text;
                string year = ddlYear.Text;
                string subj = ddlSubject.Text;
    
                String query = "select * from student " +
                    "where syear = '" + year + "' AND div ='" + division + "' order by roll";
    
                da = new System.Data.OleDb.OleDbDataAdapter(query, con);
                ds = new DataSet();
                da.Fill(ds, "student");
                maxRows = ds.Tables["student"].Rows.Count;
    
    
               
    
    
            }
    
            protected void ATTENDANCE_SUBMIT_Click(object sender, EventArgs e)
            {
               
                // Create a new HtmlTable object.
                HtmlTable table1 = new HtmlTable();
                table1.ID = ("YOURID");
    
                // Set the table's formatting-related properties.
                table1.Border = 1;
                table1.CellPadding = 1;
                table1.CellSpacing = 1;
                table1.BorderColor = "red";
    
                // Start adding content to the table.
                HtmlTableRow row;
                HtmlTableCell cell;
                int i = 0;
    
    
                for (int m = 0; m < 10; m++)
                {
                    // Create a new row and set its background color.
                    row = new HtmlTableRow();
                    row.BgColor = "lightyellow";
                    for (int j = 0; j < 5; j++)
                    {
                        if (i < maxRows)
                        {
                            // Create a cell and set its text.
                            DataRow dRow;
                            dRow = ds.Tables["student"].Rows[i];
    
                            cb1 = new CheckBox();
                      cb1.Checked = false;
    
                            cb1.ID = "" + dRow.ItemArray.GetValue(0).ToString();   //setting gr no to chechked box id//
                            cb1.Text = dRow.ItemArray.GetValue(2).ToString() + " : " +
                                dRow.ItemArray.GetValue(1).ToString();
                            cb1.Height = 50;
                            cb1.AutoPostBack = true;
                            cb1.CheckedChanged += new EventHandler
                                               (cb1_CheckedChanged);
    
                            cell = new HtmlTableCell();
    
                            cell.Controls.Add(cb1);
                            // Add the cell to the current row.
                            row.Cells.Add(cell);
                            i++;
                        }
    
                    }
    
                    // Add the row to the table.
                    table1.Rows.Add(row);
                }
    
                // Add the table to the page.
                //this.Controls.Add(table1);
              
                form1.Controls.Add(table1);
    
    
    
    
            }
    
            protected void cb1_CheckedChanged
                    (object sender, EventArgs e)
            {
                string marks;
                if (cb1.Checked)
                {
    
                    DataRow drow = ds.Tables["student"].Rows[0];  //go  to 1st row of database//
                    string variable = drow.ItemArray.GetValue(0).ToString(); //get 1st student GR_NO value in variable//
    
                    for (inc = 0; variable != cb1.ID; inc++)  //scan from 1st student GR_NO till the NON-CHECKED chckbox student GR_NO IN DATABASE//
                    {
                        drow = ds.Tables["student"].Rows[inc];
                        variable = drow.ItemArray.GetValue(0).ToString();
                    }
    
                    if (ddlSubject.Text == "SUB1")
                    {
                        marks = drow.ItemArray.GetValue(7).ToString();   //7TH COLUMN IN DATABASE IS SUBJECT1 COLUMN//
                        marks = marks + 1;                //INCREMENT THE ATTENDANCE BY 1//
                        drow[7] = marks;
                    }
                            
                
                }
                   
            }
    }
    Last edited by Rabbit; May 5 '13, 05:34 PM. Reason: Please use code tags when posting code.
Working...