简体   繁体   English

如何以声明方式将ASP.NET UserControl的public属性传递给子UserControl

[英]How do I Declaratively pass a ASP.NET UserControl's public property to a child UserControl

I'm trying to clean up an old ASP.NET app at work... 我正在尝试清理工作中的旧ASP.NET应用程序...

I've got a parent UserControl called FailedBatchPanel.ascx that is defined like: 我有一个名为FailedBatchPanel.ascx的父用户控件,其定义如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FailedBatchPanel.ascx.cs" Inherits="Controls_FailedBatchPanel" %>
<%@ Register TagPrefix="dashboard" TagName="FailedBatch" Src="~/Controls/FailedBatchControl.ascx" %>
<%@ Import Namespace="System" %>

<div class="panel panel-default" id="failedBatchesPanel">
  <div class="panel-heading">
    <h4 class="panel-title">
      <a class="accordion-toggle" data-toggle="collapse" 
         <data-parent="#<%=ParentId %>" href="#<%=PanelId %>">
        <%=Title %>
      </a>
    </h4>
  </div>
</div>
<div id="<%=PanelId %>" class="panel-collapse collapse">
  <div class="panel-body">
    <dashboard:FailedBatch runat="server" 
      ID="failedClaims" 
      BatchType='<%$ Code: BatchType %>'/>
  </div>
</div>

The child control is FailedBatch.ascx and simply contains an asp:Repeater with databinding. 子控件是FailedBatch.ascx ,仅包含具有数据绑定的asp:Repeater。

The Parent Control, FailedBatchPanel, is called as follows: 父控件FailedBatchPanel的调用方式如下:

<dashboard:FailedBatchPanel runat="server" 
    Title="SomeTitle" 
    ParentId="categories" 
    PanelId="collapseOne" 
    BatchType="goober"/>

I want to pass the BatchType defined declaratively as above to the child control that is embedded in FailedBatchPanel. 我想将以声明方式定义的BatchType传递给FailedBatchPanel中嵌入的子控件。

As you can see from the example above, I've tried using the `<%$ Code: ... %> CodeExpressionBuilder as defined by InfinitiesLoop at http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx 从上面的示例中可以看到,我尝试使用InfinitiesLoop在http://weblogs.asp.net/infinitiesloop/archive/2006/08/定义的`<%$ Code:...%> CodeExpressionBuilder 09 /在-CodeExpressionBuilder.aspx

In every case that I've tried, the BatchType public property defined in FailedBatch is always null at Page_Load 在我尝试过的每种情况下,FailedBatch中定义的BatchType公共属性在Page_Load处始终为null

I've tried: 我试过了:

  • BatchType="<%# BatchType %>" -- got Null BatchType="<%# BatchType %>" -空
  • BatchType='<%# BatchType %>' -- got Null BatchType='<%# BatchType %>' -空
  • BatchType="<%= BatchType %>" -- in this case I got the literal "<%= BatchType %>" instead of null BatchType="<%= BatchType %>" -在这种情况下,我得到的是文字“ <%= BatchType%>”而不是null
  • `BatchType="<%$ Code: BatchType %> -- got Null `BatchType =“ <%$ Code:BatchType%>-得到了空

I'm stumped and need help. 我很困惑,需要帮助。 Any ideas? 有任何想法吗?

By using the DataBind syntax we are able to get a result. 通过使用DataBind语法,我们可以获得结果。

BatchType='<%# BatchType %>'

However, as you noted this returns nothing. 但是,正如您指出的那样,此操作不会返回任何内容。 You need to include code telling the control to databind so that BatchType will be evaluated: 您需要包括告诉控件进行数据绑定的代码,以便可以评估BatchType

<% failedClaims.DataBind%>

This may be placed anywhere within your parent usercontrol. 它可以放在父用户控件内的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM