【问题标题】:How to bind repeater of Web User ControlWeb用户控件的中继器如何绑定
【发布时间】:2012-10-31 05:51:20
【问题描述】:

我真的很困惑。为此浪费了 1 天时间,但仍然无法修复。我制作了一个 (Repeater.ascx) 网络用户控件(内部有中继器),然后将其拖到 aspx 页面(Info.aspx)上这个。

   <uc1:Repeater runat="server" ID="Repeater" />

这是用户控制代码。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Repeater.ascx.cs" Inherits="DBG.Website.Billing.Repeater" %>
 <asp:Repeater ID="repeaterInvoicesPaid"    OnItemCommand="ClientNameClicked" runat="server">
               <HeaderTemplate>
            <table id="PaidInvoicesTable" cellspacing="0" cellpadding="0" style="width: 100%; font-size: 11px; border-right-width: 0; border-bottom-width: 0;"
            class="dxgvControl grid dxgvTable">
                    <thead>
                        <tr>
                            <td style="width: 150px;" class="dxgvHeader">Invoice #
                            </td>
                            <td style="width: 150px;" class="dxgvHeader">Client Name
                            </td>
                            <td style="width: 150px;" class="dxgvHeader">Invoice Date
                            </td>
                            <td style="width: 150px;" class="dxgvHeader">Due Date
                            </td>
                            <td style="width: 150px;" class="dxgvHeader">Total
                            </td>
                            <td style="width: 150px;" class="dxgvHeader">Payment Method
                            </td>
                            <td style="width: 150px;" class="dxgvHeader">Status

                        </tr>
                    </thead>
              <tbody>
            </HeaderTemplate>

            <ItemTemplate>
                <tr class="dxgvDataRow">
                    <td class="dxgv">
                    <asp:Label ID="lblInvoiceID" Text='<%#Eval("InvoiceID")%>' runat="server" /> 
                    </td>
                    <td class="dxgv">
                   <asp:LinkButton ID="btnClientName"  CommandName="NameClicked"  Text='<%#Eval("ClientName")%>' CommandArgument='<%#Eval("InvoiceReapterCustomerID") %>' runat="server"/>

                    </td>

                    <td class="dxgv">
                        <asp:Label ID="lblAddedDate" Text='<%#Eval("AddedDateAndTime")%>' runat="server" />
                    </td>
                    <td class="dxgv">
                   <asp:Label ID="lblCloseDate" Text='<%#Eval("CloseDateOnly") %>' runat="server" />

                    </td>
                    <td class="dxgv">
                    <asp:Label ID="lblRecurringCharge" Text='<%#Eval("RecurringCharge") %>' runat="server" />

                    </td>
                    <td class="dxgv">
                      <asp:Label ID="lblPaymentMethodID"  Text='<%#Eval("PaymentMethodIDInvoices")%>' runat="server" />

                    </td>
          <td class="dxgv">

           <asp:Label ID="lblStatus"   ForeColor="Green"  Text=' <%#Eval("StatusType") %>' runat="server" />

          </td>
                        <td class="dxgv">

           <asp:Label ID="lblCustomerid"   Visible="false"  Text=' <%#Eval("InvoiceReapterCustomerID") %>' runat="server" />

          </td>
                </tr>
            </ItemTemplate>

   <FooterTemplate>

            </tbody> 


       </table>
        </FooterTemplate>
        </asp:Repeater>

现在我的问题是我想在 info.cs 文件中绑定 Web 用户控件的转发器,但我不知道如何在 info.cs 上获取用户控件和转发器 ID 并绑定它。

注意:我需要将转发器与从 info.cs 的方法返回的数据源(列表)绑定。

请帮忙。 谢谢。

【问题讨论】:

    标签: c# asp.net repeater webusercontrol


    【解决方案1】:

    您可以在控件中创建一个公共方法并从页面调用它。

    在 ASCX 中

    public void BindRepeater()
    {
        //.....
        repeaterInvoicesPaid.DataSource = datatableOrDataSet;
        repeaterInvoicesPaid.DataBind();
    }
    

    在 ASPX 中

    uc1.BindRepeater();
    

    【讨论】:

    • ucl 无法识别它说“uc1 在当前上下文中不存在”。
    • 你必须给用户控件的id包含repeater,你可以在你的html中看到。 uc1 只是一个示例,提供您用于用户控制的 id。
    • right.but 我认为用户控件的控件默认情况下是受保护的或私有的,因此它给了我访问保护问题。我怎样才能将它们公开?
    • 您将需要修改其访问说明符,更好的选择可能是用户控制的公共方法,该方法将从页面调用以绑定中继器。
    • 谢谢它现在解决了...:) 最后一个问题我需要明确。实际上我需要在两个 aspx 页面上使用相同的用户控件(比如 info.aspx 和 data.aspx)。可以我在 data.cs 的 Page_Load 事件中访问 info.cs 的 Page_Load 事件?我问是因为 info.cs 在页面加载时绑定了转发器,所以我需要对 data.cs 做同样的事情。
    【解决方案2】:

    如果您指定一个 ID 和 runat="server" 属性,通常对于 vanilla ASP.NET,您可以从 aspx 页面的代码访问控件。在创建控件的位置访问 post initialize 很重要 - 您最好的选择是在 OnLoad 事件中。

    您必须记住的是,当您设置数据源/项目源时,您必须调用 .Bind() 或 .DataBind() 方法来进行实际的绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多