How to bind repeater of Web User Control
-
22-07-2021 - |
質問
I am really confused .Wasted 1 full day for it but still not able to fix .I made a (Repeater.ascx)web user control(having Repeater inside) and then drag it on aspx page(Info.aspx) like this.
<uc1:Repeater runat="server" ID="Repeater" />
This is user control code.
<%@ 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>
Now my problem is that i want to bind the repeater of web user control inside info.cs file but i do not how to get user control and repeater id on info.cs and bind it .
Note:i need to bind repeater with datasource(List) return from a info.cs's method.
Please Help . Thank you.
解決
You can make a public method in your control and call it from the page.
In ASCX
public void BindRepeater()
{
//.....
repeaterInvoicesPaid.DataSource = datatableOrDataSet;
repeaterInvoicesPaid.DataBind();
}
In ASPX
uc1.BindRepeater();
他のヒント
Typically for vanilla ASP.NET if you specify an ID and the runat="server" attribute you can access the control from the code of the aspx page. It's important to access post initialize where the controls get created - your best choice would be in the OnLoad event.
What you have to remember is when you set the datasource/itemssource, you have to call the .Bind() or .DataBind() method aftewards to do the actual binding.