Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sune Jepsen 25 posts 109 karma points
    Nov 02, 2011 @ 14:55
    Sune Jepsen
    0

    Dynamic created controls

    Hey
    Im generation some dynamic controls through a repeater control. Im binding through the itemdatabound event on the repeater:
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    PlaceHolder plhCheckboxMemberGroupsContainer = (PlaceHolder)e.Item.FindControl("plhCheckboxMemberGroupsContainer");
                    plhCheckboxMemberGroupsContainer.EnableViewState = true;
                    SiteUser siteUser = (SiteUser)e.Item.DataItem;
                    Member member = new Member(siteUser.Id);
                    List<MemberGroup> memberGroups = (from DictionaryEntry x in member.Groups select x.Value as MemberGroup).ToList();
                    CheckBox chkMemberGroup;
                    HtmlGenericControl tableCell;
                    foreach (MemberGroup item in allMemberGroups)
                    {
                        tableCell = new HtmlGenericControl("td");
                        chkMemberGroup = new CheckBox();
                        chkMemberGroup.ID = item.Text +"_" + siteUser.Id;
                        chkMemberGroup.EnableViewState = true;
                        if (memberGroups.Contains(item))
                        {
                            chkMemberGroup.Checked = true;
                       
                        }
                        tableCell.Controls.Add(chkMemberGroup);
                        plhCheckboxMemberGroupsContainer.Controls.Add(tableCell);
                    }

                }

     

    Im creating some checkboxes inside a placeholder, ive ensured that each checkbox gets a unique id and Im setting the viewstate explicit.

    But when Im looping through these repeater controls collection I cant find them, there are gone:

                 foreach (RepeaterItem item in repSiteUsers.Items)
                {

                        foreach (var control in plhCheckboxMemberGroupsContainer.Controls)
                        {
                            if (control is CheckBox)
                            {
                                // do stuff here
                            }
                        }

                }
    I have to create the controls dynamic because it varies how many there are, so I cant write them static in the mark up , which I usually do. Those controls which is are static in the mark up are found, but no the dynamic created controls.

    Any ideas?

  • Sune Jepsen 25 posts 109 karma points
    Nov 02, 2011 @ 14:58
    Sune Jepsen
    0

    Ive forgot this line of code

     PlaceHolder plhCheckboxMemberGroupsContainer = (PlaceHolder)item.FindControl("plhCheckboxMemberGroupsContainer");

    when im looping through the repeater items

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Nov 02, 2011 @ 17:27
    Tim
    0

    Are you re-binding the repeater every time the page loads?

Please Sign in or register to post replies

Write your reply to:

Draft