0

在我的项目中,最初,我提交了我想在学生数据库中访问的(学生)的年份、部门和科目。单击提交按钮后,将根据上面提交的查询创建动态复选框。但是,在处理这些复选框以更新上述提交主题的学生出勤率时,我遇到了一个问题!请帮忙!代码:
使用系统;使用 System.Collections;使用 System.Configuration;使用 System.Data;使用 System.Linq;使用 System.Web;使用 System.Web.Security;使用 System.Web.UI;使用 System.Web.UI.HtmlControls;使用 System.Web.UI.WebControls;使用 System.Web.UI.WebControls.WebParts;使用 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;
   // static int maxRows1;



   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;
            }


        }

    }

}

4

1 回答 1

0

这就是行为。当有回发时,所有变量都被重置并重新分配。

编辑:好的,问题是 cb1 只是一个变量。每次动态创建复选框时都会使用它,这意味着您不能通过简单地引用 cb1 来访问每个复选框。您需要通过分配给它的 ID 或使用用于创建它的相同逻辑来查找控件。我建议为您的表提供一个 ID,您可以使用该 ID 在后续回发中检索它。

HtmlTable table = Form1.FindControl("YourID");
for (int m = 0; m < table.Rows.Count; m++)
{
    for (int x = 0; x < table.Rows[m].Cells.Count, x++)
    {
        for (int c = 0; c < table.Rows[m].Cells[x].Controls.Count; c++)
        {
            if (typeof(table.Rows[m].Cells[x].Controls[c]) == typeof(CheckBox))
            {
                if (((CheckBox)table.Rows[m].Cells[x].Controls[c]).Checked)
                {     
                    //Your logic here
                }
            }
        }
    }
}

当您采用这种方法时,重要的是要有一种方法来识别您当前正忙于复选框的学习者,以便更新数据库。如果您需要任何澄清,请告诉我。

于 2013-04-25T06:55:15.037 回答