user2167089
user2167089

Reputation: 151

how to save dynamic check box changes

I have few check boxes that are created dynamically and existing selections will be shown but when user makes changes I want to store them back. here is the code that generates and selects dynamically

private void Role(string role)
{
   SystemUserDal dal = new SystemUserDal();
   var userId = Guid.Parse(Request.QueryString["ID"].ToString());
   var roles = dal.GetRolesList(userId);
   foreach (KeyValuePair<Guid, string> r in roles)
   {
       CheckBox chk = new CheckBox();
       chk.ID = r.Value;
       chk.Text = r.Value;
       if (role.Contains(r.Value))
       {
          chk.Checked = true;
       } 
       rolepanel.Controls.Add(chk);
   }
}

I am trying the following

private void GetCheckBoxes()
{
    foreach (Control ctrl in rolepanel.Controls)
    {
        CheckBox c = ctrl as CheckBox;
        string id = c.ID;
        string role = c.Text;
    }
}

when i step through the code it hits foreach loop with a count of 3, but the ctl is null. any clues?

Upvotes: 3

Views: 1696

Answers (5)

Floremin
Floremin

Reputation: 4089

You are probably getting the error because rolepanel.FiondControl("chk") returned null because it didn't find a control with ID="chk". Method FindControl takes a string - an ID of the control you're looking for. The checkboxes you added don't have ID="chk", they all have ID=r.value from your code. I would suggest to come up with some schema for IDs that you can use later to find the checkboxes.

If your rolepanel only contains your dynamically added checkboxes, you can just use rolepanel.Controls to get all of them.

Don't forget to cast controls to CheckBox.

So your GetCheckBoxes() could look something like:

private void GetCheckBoxes()
{
    foreach (Control ctrl in rolepanel.Controls)
    {
        if (ctrl is CheckBox)
        {
            CheckBox c = ctrl as CheckBox;
            string cText = c.Text;

            // do what you need to do with cText, or checkbox c
        }
    }
}

Upvotes: 4

jsfeb26
jsfeb26

Reputation: 1

Try using a CheckBoxList and then doing something like this

        for (int i = 0; i < chkList.Items.Count; i++)
        {
            if (chkList.Items[i].Selected)
            {
                // Store Item
            }
        }

Upvotes: 0

Dilshod
Dilshod

Reputation: 3311

Another solution is create public handler for CheckBox.CheckedChanged. Then all your dynamic CheckBoxes CheckedChanged event will be bind to that handler.

public void Checkbox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkBox = sender is CheckBox;
    if(checkbox!=null)
    {
         //do your saving things.
    }
}

Upvotes: 0

Masoud
Masoud

Reputation: 8171

you should cast control to CheckBox:

private void GetCheckBoxes()
{
    CheckBox chk = (CheckBox)rolepanel.FindControl("chk");
    if(chk!= null)
    ....

}

Upvotes: 0

chris_dotnet
chris_dotnet

Reputation: 854

Does it still error if you cast it?

e.g.

private void GetCheckBoxes()
{
    CheckBox chk = (CheckBox)rolepanel.FindControl("chk");
    if(chk!= null)

}

Upvotes: 2

Related Questions