动态Checkbox事件处理程序asp.net

时间:2012-10-29 21:31:27

标签: asp.net .net c#-4.0

我发现了一些与此相关的问题,但没有一个我可以弄清楚如何申请我的问题。无论如何,我有一个ASP网页表单,它从数据库中提取问题并将它们放在一个表格中。在表格中,我有一个带有复选框的列,用户可以在其中标记问题。我的问题是CheckChanged事件的事件处理程序没有触发。我读了一些关于回发和诸如此类的东西,但我的问题是在按下检索问题按钮之前不会创建这些控件。任何帮助或指示都会很棒。

谢谢,

约瑟夫

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ScienceAssessmentToolASP
{
public partial class createnewtest : System.Web.UI.Page
{
    private int n;
    private SqlConnection conn = null;
    private List<int> flaggedQuestions = new List<int>();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            GetConn();
            ExecuteRetrieval();
            n = 1;
        }
        catch (Exception ex) { n = 0; Response.Write("for debugging: " + ex); }
        finally { if (conn != null) conn.Close(); }
        if (n < 0)
            //Label1.Text = "Connection Successful";
            Label3.Text = "Failed to Connect to Database, please contact the administrator.";           
    }
    private void GetConn()
    {
        string connString = @"
            removed ";

        conn = new SqlConnection(connString);
        conn.Open();
    }

    private void ExecuteRetrieval()
    {
        List<string> names = new List<string>(),
          types = new List<string>();
        SqlDataReader reader = null;

         string query = "select * from [ScienceQA] where [GradeLevel] = " + DropDownList1.Text +
            " and [Topic] = '" + DropDownList2.Text + "';";

        SqlCommand cmd = new SqlCommand(query, conn);

        reader = cmd.ExecuteReader();

        TableHeaderRow headerRow = new TableHeaderRow();
        TableHeaderCell idH = new TableHeaderCell();
        TableHeaderCell questionH = new TableHeaderCell();
        TableHeaderCell answerH = new TableHeaderCell();
        TableHeaderCell flagH = new TableHeaderCell();

        idH.Text = "ID";
        questionH.Text = "Question";
        answerH.Text = "Answer";
        flagH.Text = "Flag";

        headerRow.Cells.Add(idH);
        headerRow.Cells.Add(questionH);
        headerRow.Cells.Add(answerH);
        headerRow.Cells.Add(flagH);
        resultTable.Controls.Add(headerRow);

        while (reader.Read())
        {
            TableRow row = new TableRow();
            TableCell idCell = new TableCell();
            TableCell qCell = new TableCell();
            TableCell aCell = new TableCell();
            TableCell flag = new TableCell();

            idCell.Text = reader[0].ToString();
            qCell.Text = reader[1].ToString();
            aCell.Text = reader[2].ToString();

            CheckBox flagBox = new CheckBox();
            flagBox.ID = "flag" + idCell.Text.ToString();
            //flagBox.Text = "Flag";
            flagBox.CheckedChanged += new System.EventHandler(flagButton_Click);
            flag.Controls.Add(flagBox);

            row.Cells.Add(idCell);
            row.Cells.Add(qCell);
            row.Cells.Add(aCell);
            row.Cells.Add(flag);
            resultTable.Controls.Add(row);          
        }

        Label4.Visible = true;
        flagCounter.Visible = true;
        resultTable.Visible = true;
    }
    protected void flagButton_Click(object sender, EventArgs e) 
    {
        CheckBox lb = (CheckBox)sender;
        int questionID = Convert.ToInt32(lb.Text.Substring(4));

        if (lb.Checked)
        {
            lb.Checked = false;
            flaggedQuestions.Add(questionID);
            flagCounter.Text = Convert.ToString(Convert.ToInt32(flagCounter.Text) - 1);
        }
        else
        {
            lb.Checked = true;
            flaggedQuestions.Remove(questionID); 
            flagCounter.Text = Convert.ToString(Convert.ToInt32(flagCounter.Text) + 1);
        }          
    }
}

}

2 个答案:

答案 0 :(得分:0)

创建控件时,请尝试将AutoPostBack设置为true:

flagBox.AutoPostBack = true;

这使得事件将导致回发。如果您没有此代码,则在您提交表单之前不会触发代码。

答案 1 :(得分:0)

我明白了。我想我在按钮处理程序中不需要任何东西,因为点击按钮会导致回发。所以我把所有的button1处理程序的东西都放在我的pageload中,并使用if(postback)检查并且这样做有效。