动态复选框未通过C#在asp.net中检查

时间:2013-04-10 19:43:54

标签: c# asp.net

我动态生成复选框,如果两个查询的o / p匹配,则最初2个查询匹配,然后在这种情况下检查并禁用特定复选框.............在我的情况下,对应于匹配的o / p查询的匹配复选框被禁用但不会被检查

protected void Page_Load(object sender, EventArgs e)
{
            DataSet ds = c.readDataSet("select teacher_id, name from teacher");
            DataTable dt = ds.Tables[0];
            foreach (DataRow drow in dt.Rows)
            {
                    chk = new CheckBox();
                    chk.ID = "chk_" + drow[0].ToString();

                    SqlDataReader dr = c.readData("select t.teacher_id from teacher t join coordinator c on t.teacher_id=c.teacher_id where c.branch='" + ddl_class.Text + "' and c.cyear=" + DateTime.Now.ToString("yyyy")+" and c.status=2" );
                    while (dr.Read())
                    {
                        if(dr.GetString(0)==drow[0].ToString())
                        {
                           chk.Checked = true;
                           chk.Enabled = false;
                           count = 1;
                        }
                    }

                    dr.Close();

                    chk.ToolTip = drow[1].ToString();
                    lb = new Label();
                    lb.Text = drow[1].ToString();
                    PnlCtrl.Controls.Add(chk);
                    PnlCtrl.Controls.Add(lb);
                    PnlCtrl.Controls.Add(new LiteralControl("<br/>"));
            }

}

1 个答案:

答案 0 :(得分:0)

好的,这是因为当OnPreRender设置为CheckBoxEnabled控件的false会忽略它:

protected internal override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    bool autoPostBack = this.AutoPostBack;
    if (this.Page != null && base.IsEnabled)
    {
        this.Page.RegisterRequiresPostBack(this);
        if (autoPostBack)
        {
            this.Page.RegisterPostBackScript();
            this.Page.RegisterFocusScript();
            if (this.CausesValidation && this.Page.GetValidators(this.ValidationGroup).Count > 0)
            {
                this.Page.RegisterWebFormsScript();
            }
        }
    }
    if (!this.SaveCheckedViewState(autoPostBack))
    {
        this.ViewState.SetItemDirty("Checked", false);
        if (this.Page != null && base.IsEnabled)
        {
            this.Page.RegisterEnabledControl(this);
        }
    }
}

这真的不是那么令人惊讶。因此,而不是设置Checked = true执行此操作:

chk.Attributes.Add("checked", "checked");