如何从主页面代码绑定嵌套控件

时间:2009-09-10 01:14:23

标签: asp.net vb.net binding

我有以下结构,需要重新绑定较低的控件(DropDownList) 从MainPage背后的代码。

    x MainPage1 x---- Panel1 (modal popup)
    x--------- UpdatePanel (upMailOrStatusAction, on Panel1)
    x-------------- RadioButtonList (rblActionLevel, on UpdatePanel)
    x-------------- SubForm1 (on Panel1)
    x------------------- CustomControl1 (on Subform1)
    x------------------------ DropDownList (on CustomControl1)

实现这一目标的正确方法是什么?

我在控件中添加了一个公共方法“BindMailActionLookup()”,但是如何从主页面调用它?我得到“在当前背景下不存在”?

以下是子表单的标记:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MailAddSubform.ascx.cs" 
Inherits="Company.Solutions.Web.Controls.MailAddSubform" %>

<%@ Register TagPrefix="st" TagName="MailActionLookup" Src="~/Controls/StMailActionLookup.ascx" %>
<div class="NinetyNinePercentWide">
    <div class="NinetyNinePercentWide EightPixelBottomMargin">
        <div class="RowHeader" style="padding-top: 20px;">
            <span class="labelfield" >Action:</span>
        </div>
        <div>
            <st:MailActionLookup ID="mailActionLookup" runat="server" />
        </div>
    </div>
    <div class="NinetyNinePercentWide EightPixelBottomMargin" >
        <br class="NinetyNinePercentWide" Text="&nbsp" />
        <div class="RowHeader" >
            <span class="labelfield" >Message:</span>    
        </div>
        <div class="TwelvePixelLeftPad" >
            <asp:TextBox ID="txtMailActionMessage" runat="server" MaxLength="40" />
        </div>
    </div>
</div>

以下是自定义控件的标记:

<%@ Control Language="C#" AutoEventWireup="true"        CodeBehind="StMailActionLookup.ascx.cs"     Inherits="Company.Solutions.Web.Controls.StMailActionLookup" %>


<div id="mainControlContainer" style="width:99%; padding:8px;">

<div id="comboContainer" style="float:left; padding-top:12px;padding-left:5px; padding- right:5px; padding-bottom:3px;">
  <asp:UpdatePanel runat="server" ID="mailActionUpdater">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="chkForms" EventName="CheckedChanged" />
        <asp:AsyncPostBackTrigger ControlID="chkRequested" EventName="CheckedChanged" />
        <asp:AsyncPostBackTrigger ControlID="chkOther" EventName="CheckedChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:DropDownList runat="server" ID="ddlLookup" width="240px" ondatabound="ddlLookup_DataBound1" /> 
    </ContentTemplate>
  </asp:UpdatePanel>


</div>

<div id="filterContainer" style="text-align:left;padding-left:6px;width:275px">
    <fieldset style="width:260px;">
        <legend>Filters</legend>
        <asp:CheckBox ID="chkForms" runat="server" Text="Forms" AutoPostBack="true" />
        <asp:CheckBox ID="chkRequested" runat="server" Text="Requested Info" AutoPostBack="true" />
        <asp:CheckBox ID="chkOther" runat="server" Text="Other" AutoPostBack="true" />
    </fieldset>

</div>    
</div>

以下是我添加公共方法的代码的一部分:

namespace Company.Solutions.Web.Controls
{
    public partial class StMailActionLookup : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           BindForm();
        }

        public void BindMailActionLookup()
        {
            BindForm();
        }

        protected void BindForm()
        {
            GetActionLevel();
            IEnumerable actions = GetClaimMailActions(GetFilter());
            ddlLookup.DataSource = actions;
            ddlLookup.DataTextField = "CodeAndDescription";
            ddlLookup.DataValueField = "ActionCd";
            ddlLookup.DataBind();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您不应该将CustomControl1的内部暴露给消费者,因此最正确的方法是在主页可以调用的CustomControl1上公开一个公共方法(可能称之为“ResetDropDowns”)。

CustomControl1知道它自己的下拉列表,因此当有人调用该方法时,它可以轻松找到并重新绑定控件。

答案 1 :(得分:0)

好的,我们有一个解决方案,感谢“womp”的建议和我的一位同事。 只需将公共呼叫嵌套在一个链中:

这主要在索赔信息代码背后:

    // Rebind the action code drop down to restrict to base level 
    mailAddSubform.BindMailActionLookup();

然后在子窗体代码后面:

    public void BindMailActionLookup()
    {
        mailActionLookup.BindMailActionLookup();
    }

最后,这在查找控件中:

  public void BindMailActionLookup()
    {
        BindForm();
    }