【问题标题】:How do I find a repeater that is inside a user control如何找到用户控件内的中继器
【发布时间】:2019-01-04 18:09:51
【问题描述】:

我需要能够导出到 excel 中继器。那部分没有问题。我有相应的代码。问题是我需要导出的中继器在用户控件中。我有一个加载多个报告的页面,每个报告都是不同的用户控件。

用户控件本身嵌套在一个 div 标签中:

<div id="report_div" runat="server" style="width:100%;">
    <uc:report1 id="UCReport1" runat="server" />
    <uc:report2 id="UCReport2" runat="server" />
</div>

所以我要查找的是用户控件中的中继器,因此我可以将其渲染以导出到 excel。

【问题讨论】:

  • 你可以访问用户控件背后的代码吗?您是否考虑过使用 Controls 属性?
  • @the_lotus 我也有用户控件。父页面加载报告。老实说,我不知道我使用过 Controls 属性
  • 如果您有权访问用户控件,那么您可以添加一个属性,将访问中继器的权限授予其他人。你最终可能会得到 UCReport1.TheRepeater

标签: asp.net vb.net user-controls


【解决方案1】:

我认为这取决于您何时想要获取数据。您可以在转发器的 DataBind() 上执行此操作,并在填充数据时立即获取您需要的数据。或者,如果您以后需要它,我想您可以在控件本身中创建一个动作事件并传回转发器..

    //in your User control class that holds the repeater..
    public static event Action<Repeater> GetRepeater;
    private Repeater repeater1;

    //populate and display repeater omited...

    private void onGetRepeater(Repeater  repeater)
    {
            GetRepeater?.invoke(repeater);
    }
    //just fire from somewhere. you 
    //could use the dataBind() page_Load, button click or whatever.
    //depending on when you need it. 
    protected void someBtnInYourControl_Click(object sender, EventArgs eArgs)
    {
          onGetRepeater(this.repeater1);
    }

现在您可以在项目的其他任何地方订阅。

      public class OtherController : Controller
      {
    public ActionResult Index()
    {
        //subscribe to the event

        UCReport1.GetRepeater += UCReport1_GetRepeater;
        return View();
    }

    private void UCReport1_GetRepeater(System.Web.UI.WebControls.Repeater foo)
    {
        //when your event is trigger on the UC it will pass the repeater back so you 
          can use however.

         exportRepeaterToReport(foo);
    }
     }

【讨论】:

    【解决方案2】:

    您可以使用 UserControl 上的 FindControl 来定位中继器。

    Repeater repeater = UCReport1.FindControl("Repeater1") as Repeater;
    

    现在您可以使用repeater 访问所有Repeater 属性。

    int items = repeater.Items.Count;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 2018-01-24
      • 2010-12-16
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多