Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从父页的Web用户控件中的元素获取值_C#_Asp.net_User Controls - Fatal编程技术网

C# 从父页的Web用户控件中的元素获取值

C# 从父页的Web用户控件中的元素获取值,c#,asp.net,user-controls,C#,Asp.net,User Controls,我有一个包含复选框列表的Web用户控件 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

我有一个包含复选框列表的Web用户控件

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>

如何从父页引用用户控件中的控件

我会将此信息作为用户控件上的公共属性公开,只返回必要的信息,例如:

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(); }
}
使用System.Collections.Generic;
使用System.Linq;
// ...
公共IList校验值
{
获取{return chkList.Items.Cast().Where(i=>i.Selected)。选择(i=>i.Value)。ToList();}
}

我会将此信息作为用户控件上的公共属性公开,只返回必要的信息,例如:

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(); }
}
使用System.Collections.Generic;
使用System.Linq;
// ...
公共IList校验值
{
获取{return chkList.Items.Cast().Where(i=>i.Selected)。选择(i=>i.Value)。ToList();}
}

将属性封装在控件后面的代码中

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



注意-请添加必要的名称空间,如
System.Collections.Generic
System.Linq
将属性封装在控件后面的代码中

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



注意-请添加必要的名称空间,如
System.Collections.Generic
System.Linq

这应该可以正常工作。。。在您的父页面的codebehind中,是否可以识别您的userControl的名称?这应该可以正常工作。。。在父页面的codebehind中,是否可以识别userControl的名称?