3

listviewからに動的な情報を渡したいのですUserControlが、何かが足りないと思います。

.aspxページ:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource"
        DataKeyNames="id_Image">
  <ItemTemplate>
       <uc1:Info Name_Lbl='<%# Bind("Name") %>'   Description_Lbl='<%# Bind("Description")%>' ID="info1"  runat="server" />
  </ItemTemplate>
</asp:ListView>

.ascxファイル:

Name:
<asp:Label ID="NameLabel" runat="server"  />
Description:
<asp:Label ID="DescriptionLabel" runat="server"  />

.ascx codebehindファイル:

public string Name_Lbl { get; set; }
public string Description_Lbl { get; set; }

protected void Page_Load(object sender, EventArgs e)
{  
    NameLabel.Text = Name_Lbl;
    DescriptionLabel.Text = Description_Lbl;  
}

Imが次のような文字列テキストから値を取得しようとすると、すべてが正常に機能します。

<uc1:Info Name_Lbl="Name"   Description_Lbl="Description" ID="info1"  runat="server" />

しかし、から値を取得しようとするとDatasource、usercontrolの文字列値は「null」になります。誰かが私が間違っていることを確認できますか?ありがとう、ジムオーク

4

2 に答える 2

4

DataBindingは、コントロールライフサイクルのかなり後の段階で発生しますLoad

ロード時にテキストを割り当てますが、コントロールは次のテキストのみを受け取りますDataBind

これを修正するには、テキストを設定しますOnPreRender。これは後に発生しますDataBind

protected override void OnPreRender(EventArgs e)
{  
    NameLabel.Text = Name_Lbl;
    DescriptionLabel.Text = Description_Lbl;  
}
于 2013-03-13T10:14:33.517 に答える
1

コード内のすべてが正常に見えるのは、コード行を確認するだけです。

<uc1:Info Name_Lbl='<%# Bind("Name") %>'   Description_Lbl='<%# Bind("Description"%>' ID="info1"  runat="server" />

Description_Lblに対する閉じ括弧「)」がありません。そのはず:

<uc1:Info Name_Lbl='<%# Bind("Name") %>'   Description_Lbl='<%# Bind("Description")%>' ID="info1"  runat="server" />
于 2013-03-13T09:55:07.190 に答える