如何在另一个按钮代码中访问动态创建的复选框?

时间:2013-04-25 06:19:04

标签: c# asp.net

在我的项目中,我最初提交了我想要在学生数据库中访问的年份,部门和学科(学生)。单击“提交”按钮后,将根据上面提到的查询创建动态复选框。 但是,我在遇到问题的同时处理这些复选框以更新学生对上述提交主题的出勤率! 请帮忙!     代码:
    使用系统;     使用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;
            }


        }

    }

}

1 个答案:

答案 0 :(得分:0)

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

编辑:好的问题是cb1只是一个变量。每次动态创建复选框时都会使用它,这意味着您无法通过简单地引用cb1来访问每个复选框。您需要通过分配给它的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
                }
            }
        }
    }
}

采用这种方法时,重要的是要有一种方法来识别您当前正忙着的复选框,以便更新数据库。如果您需要任何明确的信息,请告诉我。

相关问题