0

我正在动态生成复选框,如果两个查询的 o/p 匹配,则最初 2 个查询 r 匹配,然后在这种情况下,特定复选框被选中并禁用......但在我的情况下与查询的匹配 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/>"));
            }

}
4

1 回答 1

0

好的,发生这种情况是因为控件OnPreRender在设置CheckBox为时会忽略它:Enabledfalse

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");
于 2013-04-10T19:58:57.310 回答