2013-03-13 44 views
3

我想从listview一些动态信息传递给UserControl里面,但我想我失去了一些东西。传递变量用户控件列表视图(asp.net,C#)

.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代码隐藏文件:

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; 
} 

一切工作正常,如果我试着去得到字符串值文字,如:

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

但是,当我试图从Datasource获得值时,usercontrol中的字符串值为“null” 任何人都可以看到我在做什么错了吗?谢谢你,吉姆·橡树

+0

谢谢。我有完全相同的问题。 – Pitarou 2013-05-23 14:00:52

回答

4

DataBinding在控制生命周期中比Load晚很多。

您将其分配上加载文本,但你的控制只接收在DataBind

文本为了解决这个问题,你可以设置你的文字OnPreRender。这发生在DataBind

protected override void OnPreRender(EventArgs e) 
{ 
    NameLabel.Text = Name_Lbl; 
    DescriptionLabel.Text = Description_Lbl; 
} 
+0

谢谢,那帮了我! – Oak 2013-03-13 10:21:20

+0

欢迎您! – nunespascal 2013-03-13 10:23:32

+0

我也有同样的问题。谢谢! – Pitarou 2013-05-23 14:00:32

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" /> 
+0

嗨,对不起,我在创建这个主题时做了这个。 – Oak 2013-03-13 10:00:23

+0

你检查过aspx代码后面的数据源吗?结果集有这些字段“名称”和“描述”的值? – 2013-03-13 10:06:57

+0

是的,如果我在itemtemplate中粘贴.ascx代码,一切正常。当我将变量传递给usercontrol时,我想我会错过一些东西。 – Oak 2013-03-13 10:10:28