2016-11-11 44 views
0

我有一個用戶控件的值:用戶控件不顯示暴露控制

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="StratPlan.Main.UserCons.WebUserControl1" %> 

<div> 
    <table> 
     <tr> 
      <td>title: </td> 
      <td> 
       <asp:TextBox ID="TitleTextBox" runat="server"/> 
      </td> 
     </tr> 
     <tr> 
      <td>strategy id: </td> 
      <td> 
       <asp:TextBox ID="StrategyIdTextBox" runat="server"/> 
      </td> 
     </tr> 
     <tr> 
      <td>company: </td> 
      <td> 
       <asp:TextBox ID="CompanyTextBox" runat="server"/> 
      </td> 
     </tr> 
    </table> 
</div> 

在其背後的代碼:

public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     TitleTextBox.Text = ExpTitle; 
     StrategyIdTextBox.Text = ExpStrategyId; 
     CompanyTextBox.Text = ExpCompany; 
    } 

    public string ExpTitle 
    { 
     get { return this.TitleTextBox.Text; } 
    } 

    public string ExpStrategyId 
    { 
     get { return this.StrategyIdTextBox.Text; } 
    } 

    public string ExpCompany 
    { 
     get { return this.CompanyTextBox.Text; } 
    } 

} 

然後在我的頁面上,我有一個列表視圖:

<div> 
    <asp:ListView ID="ListView1" runat="server"> 
     <ItemTemplate> 
      <uc1:WebUserControl1 runat="server" ID="WebUserControl1" ExpStrategyId='<%# Bind(StrategyId) %>' ExpTitle='<%# Bind(Title) %>' ExpCompany='<%# Bind(CompanyName) %>'/> 
     </ItemTemplate> 
    </asp:ListView> 
</div> 

我在代碼綁定數據源這樣的背後:

public void LoadGridView() 
{ 
    localVm.EntityList = localVm.RetrieveMany(localVm.SearchItem); 
    ItemsGridView.AutoGenerateColumns = false; 
    ListView1.DataSource = localVm.EntityList; 
    ListView1.DataBind(); 
} 

但是,每當我去我的網頁,它不會給用戶控件的價值。我究竟做錯了什麼?

回答

1

頁面Load事件在數據綁定控件綁定到它們的數據之前被調用。將其更改爲PreRenderCompleteRender這樣

public partial class WebUserControl1 : System.Web.UI.UserControl 
{ 
    protected void Page_PreRenderComplete(object sender, EventArgs e) 
    { 
     TitleTextBox.Text = ExpTitle; 
     StrategyIdTextBox.Text = ExpStrategyId; 
     CompanyTextBox.Text = ExpCompany; 
    } 
    ... 
} 

看看ASP.NET Page Life Cycle Overview