【问题标题】:Get values from element in a Web User Control from the parent page从父页面的 Web 用户控件中的元素获取值
【发布时间】:2012-03-28 20:22:07
【问题描述】:

我有一个包含 CheckBoxList 的 Web 用户控件。

我的列表控件:

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

我想检查在父页面上提交表单之前检查了哪些项目。我希望它会像这样工作:

我的父页面:

MyUserControlInstance.chkList.Items.Count

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

【问题讨论】:

  • 这应该可以工作...在您的 ParentPage 的代码隐藏中,您的 userControl 的名称是否被识别?

标签: c# asp.net user-controls


【解决方案1】:

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

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

现在换行

MyUserControlInstance.chkList.Items.Count

MyUserControlInstance.GetLength


注意 - 请添加必要的命名空间,例如 System.Collections.GenericSystem.Linq

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      相关资源
      最近更新 更多