[英]is that possible to add controls into WebUserControl from aspx page?
[英]DataList is not loading in my aspx page from my webusercontrol
我有一個看起來像這樣的網絡用戶控件
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HtmlReport.ascx.cs" Inherits="HtmlReport" %>
<asp:Label runat="server" Text="REPORT DATALST" />
<asp:DataList runat="server" ID="DataListNames">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label runat="server" Text='<%# Eval("CustomerName") %>' />
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" Text='<%# Eval("ProjectName") %>' />
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" Text='<%# Eval("LastName") %>' /><p>, </p>
<asp:Label runat="server" Text='<%# Eval("FirstName") %>' />
</td>
</tr>
</table>
<asp:DataList runat="server" ID="DataListHours">
<ItemTemplate>
<table>
<tr>
<asp:Label runat="server" Text='<%# Eval("HoursWorked") %>' />
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
<FooterTemplate>
<asp:Label runat="server" Text="Total Hours " /><asp:Label runat="server" Text='<%# Eval("TotalHoursWorked") %>' />
</FooterTemplate>
</asp:DataList>
.cs
protected void Page_Load(object sender, EventArgs e)
{
}
public void GenerateReport(IList<DataObjects.Fixedfeereport> FixedFeeReports)
{
DataListNames = new DataList();
DataListNames.DataSource = FixedFeeReports;
DataListNames.DataBind();
}
在我的內容頁面.cs中
HtmlReport rpt = new HtmlReport();
rpt.GenerateReport(Reports);
//Reports is a data object that holds all the fields from the webusercontrol datalist, ex: CustomerName, ProjectName, FirstName, LastName, etc....
Control test = Page.LoadControl("HtmlReport.ascx");
PanelReport.Controls.Add(test);
我的問題是,當我將webuserControl加載到原始內容頁面上時,將其稱為default.aspx / default.cs,它將獲取我具有“ REPORT DATALIST”的Label,並顯示該標簽-很酷。 但是,它獲取了數據列表,並且為空。 不包含任何數據,即使我為它提供了一個數據源並在.cs中進行了數據綁定
我以前沒有使用過這樣的webusercontrols,所以有人可以指出正確的方向嗎?
我解決了自己的問題,實際上是將webusercontrol從解決方案資源管理器中移到了aspx頁面上,然后將其拖放了。 這使我在default.aspx的頂部
<%@ Register Src="~/HtmlReport.ascx" TagName="Report" TagPrefix="R" %>
<%@ Reference VirtualPath="~/HtmlReport.ascx" %>
這是在我的面板中,我將其放入。
<R:Report ID="Report1" runat="server" />
我將contentpage.cs更改為如下形式,
Report1.GenerateReport(Reports);
最后我刪除了
DataListNames = new Datalist();
在我的webusercontrol.cs中,它可以工作並呈現我的數據列表!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.