tags:

views:

191

answers:

4

In my asp.net application i have created checkboxes at runtime. I want to disable the checked checkbox when i click on a button. How do I achieve this? Here is my code:

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;
        }           
    }
}
A: 

Maybe

RoleCheckBox.Parent.Controls.Remove(RoleCheckBox);
cwap
+2  A: 

One rule-of-thumb while dealing with dynamically generated user controls is that you have to add them to the container on EVERY POSTBACK.

Just modify your code to generate the controls on every postback, and your code will start working like a charm!

EDIT

I am unsure what the ch variable is. I have assumed that its a checkbox. If I am correct, then all you have to do is to modify these lines

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

to this

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

EDIT 2

This is the code for generating 10 checkboxes dynamically -

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);
    }
}

Check the checkboxes when the page renders on the client side, click the button. This will cause a postback and the checkboxes will be generated again. In the click event of the button, you'll be able to find the checkboxes like this -

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.
    }
}

I hope this is clear.

Kirtan
sir i have done the same, but i am not geting the outputthanks
sangita
A: 
 protected void Page_Load(object sender, EventArgs e)
    {
        TableRow tr;
        TableCell tc;
        CheckBox ch;
        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();
                ch.CheckedChanged += new EventHandler(ch_CheckedChanged);
                tc.Controls.Add(ch); 
                tr.Cells.Add(tc); 
            } 
            Table1.Rows.Add(tr); 
        }

    }

    void ch_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox c = sender as CheckBox;
        c.Enabled = !c.Checked;
    }
adatapost
A: 

You are not getting a proper reference to your checkboxes - mRoleCheckBoxList is not mentioned as part of your checkbox creation.

Try the following in your button event handler:

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

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

related questions