禁用在运行时创建的复选框

时间:2009-08-11 10:38:41

标签: asp.net-2.0

在我的asp.net应用程序中,我在运行时创建了复选框。我想在单击按钮时禁用选中的复选框。我该如何实现这一目标?这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{        
    for (int i = 0; i < 12; i++)
    {
        tr = new TableRow();
        tr.BorderStyle = BorderStyle.Groove;
        for (int j = 0; j < 18; j++)
        {
            tc = new TableCell();
            tc.BorderStyle = BorderStyle.Groove;
            ch = new CheckBox();
            tc.Controls.Add(ch);
            tr.Cells.Add(tc);
        }
        Table1.Rows.Add(tr);
    }

    if(!IsPostBack)
    {
        form1.Controls.Add(ch);
        mRoleCheckBoxList.Add("RoleName", ch);
    }
}




protected void Button1_Click(object sender, EventArgs e)
{        
    IDictionaryEnumerator RoleCheckBoxEnumerator = mRoleCheckBoxList.GetEnumerator();
    while (RoleCheckBoxEnumerator.MoveNext())
    {
        CheckBox RoleCheckBox = (CheckBox)RoleCheckBoxEnumerator.Value;
        string BoxRoleName = (string)RoleCheckBox.Text;
        if (RoleCheckBox.Checked == true)
        {
            RoleCheckBox.Enabled = false;
            break;
        }           
    }
}

4 个答案:

答案 0 :(得分:2)

在处理动态生成的用户控件时,一个经验法则是您必须在每次回传上将它们添加到容器中。

只需修改代码即可在每次回发时生成控件,您的代码就会像 魅力 一样开始工作!

修改

我不确定ch变量是什么。我假设它是一个复选框。如果我是对的,那么你所要做的就是修改这些行

if(!IsPostBack)
{
    form1.Controls.Add(ch);
    mRoleCheckBoxList.Add("RoleName", ch);
}

到这个

//if(!IsPostBack)
{
    form1.Controls.Add(ch);
    mRoleCheckBoxList.Add("RoleName", ch);
}

编辑2

这是动态生成10个复选框的代码 -

protected void Page_Init(object sender, EventArgs e)
{
    CheckBox c = null;

    for (int i = 0; i < 10; i++)
    {
        c = new CheckBox();
        c.ID = "chk" + i.ToString();
        c.Text = "Checkbox " + (i + 1).ToString();

        container.Controls.Add(c);
    }
}

选中客户端页面呈现时的复选框,单击按钮。这将导致回发,并将再次生成复选框。在按钮的点击事件中,您将能够找到这样的复选框 -

protected void Button1_Click(object sender, EventArgs e)
{
    CheckBox c = null;

    for (int i = 0; i < 10; i++)
    {
        c = container.FindControl("chk" + i.ToString()) as CheckBox;
        //Perform your relevant checks here, and disable the checkbox.
    }
}

我希望这很清楚。

答案 1 :(得分:0)

也许

RoleCheckBox.Parent.Controls.Remove(RoleCheckBox);

答案 2 :(得分:0)

您没有正确引用复选框 - 在复选框创建过程中未提及mRoleCheckBoxList。

在按钮事件处理程序中尝试以下操作:

foreach (TableRow row in Table1.Rows)
    foreach (TableCell cell in row.Cells)
    {

        CheckBox check = (CheckBox)cell.Controls[0];
        if (check.Checked) check.Enabled = false;
    }

答案 3 :(得分:0)

始终在.Net中创建动态控件时,请在此处创建它们:      protected override void CreateChildControls()      {

     base.CreateChildControls();
     this.CreateDynamicControls();

 } //eof method 

并在PostBack中从事件触发器或非动态控件中找到它们:

    /// <summary>
    /// Search for a control within the passed root control by the control id passed as string
    /// </summary>
    /// <param name="root">the upper control to start to search for</param>
    /// <param name="id">the id of the control as string</param>
    /// <returns></returns>
    public virtual Control FindControlRecursively(Control root, string id)
    {
        try
        {
            if (root.ID == id)
            {
                return root;
            } //eof if

            foreach (Control c in root.Controls)
            {
                Control t = this.FindControlRecursively( c, id);
                if (t != null)
                {
                    return t;
                } //eof if
            } //eof foreach
        } //eof try 
        catch (Exception e)
        {

            return null;
        } //eof catch

        return null;
    } //eof method