简体   繁体   English

如何添加动态数据绑定用户控件?

[英]How do i add dynamic databound user controls?

So i've got a user control called TagFilter which has 2 repeaters. 所以我有一个名为TagFilter的用户控件,它具有2个转发器。 The control will be added to a page N times, with every repeater bound to a different data table. 该控件将被添加到页面N次,每个转发器都绑定到一个不同的数据表。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TagFilter.ascx.cs" Inherits="Hite.Web.Controls.TagFilter" %>
<div class="tagDiv">
<h3>
    <span style="">Results By :</span>
    <asp:Repeater ID="rptUsed" runat="server">
        <ItemTemplate>
            <span class="UsedTags">
                <asp:ImageButton ID="ImageButton1" runat="server" OnCommand="UsedPops_Remove" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "tagid") %>'
                    ImageUrl="~/Images/errorIcon.png" />
                <a href="#">
                    <%# DataBinder.Eval(Container.DataItem, "tagname") %></a> </span>
        </ItemTemplate>
    </asp:Repeater>
</h3>
<asp:Repeater ID="rptUnused" runat="server">
    <ItemTemplate>
        <b style="padding-left: 5px; padding-right: 5px;">
            <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="lbPop_Command" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "tagid") %>'><%# DataBinder.Eval(Container.DataItem, "tagname") %>
            </asp:LinkButton></b>
    </ItemTemplate>
</asp:Repeater>
<br />
</div>

public partial class TagFilter : System.Web.UI.UserControl
{
    public DataTable UsedDT { get; set; }
    public DataTable UnusedDT { get; set; }
    public SearchParameters searchParams { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        rptUsed.DataSource = UsedDT;
        rptUsed.DataBind();

        rptUnused.DataSource = UnusedDT;
        rptUnused.DataBind();
    }

I loop through with a dataset that has multiple data tables and try to create the user controls 我遍历具有多个数据表的数据集,并尝试创建用户控件

            TagFilter tf = (TagFilter)Page.LoadControl("/Controls/TagFilter.ascx");
            tf.UnusedDT = ds.Tables[3];
            tf.UsedDT = ds.Tables[4];
            tagdiv.Controls.Add(tf);

When i step through the code in the debugger, the data table has rows when it's assigned to the control, but has no rows on page_Load. 当我单步调试器中的代码时,将数据表分配给控件时,该表有行,但page_Load上没有行。 Can someone please help me find my error? 有人可以帮我找到我的错误吗? Is it a page lifecycle issue? 是页面生命周期问题吗?

You should create a method in the usercontrol. 您应该在用户控件中创建一个方法。

public void BindData()
{
    rptUsed.DataSource = UsedDT;
    rptUsed.DataBind();

    rptUnused.DataSource = UnusedDT;
    rptUnused.DataBind();
}

and then call 然后打电话

tf.BindData();

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

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