0

I have a Web User Control that contains a CheckBoxList.

MyListControl:

<asp:checkboxlist id="chkList" runat="server">
     <asp:listitem id="option1" runat="server" value="Madrid" />
     <asp:listitem id="option2" runat="server" value="Oslo" />
     <asp:listitem id="option3" runat="server" value="Lisbon" />
</asp:checkboxlist>

I want to check what Items are checked before a form is submitted on the parent page. I was hoping it would work like this:

myParentPage:

MyUserControlInstance.chkList.Items.Count

How does one reference a control in a user control from the parent page?

1
  • This should be working... in your ParentPage's codebehind, is the name of your userControl recognized? Commented Mar 28, 2012 at 20:27

2 Answers 2

3

Encapsulate the property in your code behind of the control..

public int GetLength
{
    get
    {
        return CheckListBox.Items.Cast<ListItem>().Where(
                                     i => i.Selected).ToList().Count;
    }
}

Now replace the line

MyUserControlInstance.chkList.Items.Count

with MyUserControlInstance.GetLength


Note - Please add necessary Namespaces like System.Collections.Generic and System.Linq

Sign up to request clarification or add additional context in comments.

Comments

2

I would expose this information as a public property on the User Control, returning only the necessary information, such as:

using System.Collections.Generic;
using System.Linq;

// ...

public IList<string> CheckedValues
{
    get { return chkList.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToList(); }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.