我有一个将数据绑定到转发器的 ASP.NET 控件。在该中继器内,我有另一个自定义用户控件。我想根据当前绑定项将值传递给第二个控件。
<asp:Repeater runat="server" ID="ProductList">
<ItemTemplate>
<p>Product ID: <%# Eval("ProductID") %></p>
<myControl:MyCoolUserControl runat="server" ProductID='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:Repeater>
转发器项目模板使用 Eval 语句正确打印出我的产品 ID,但是当我执行相同的操作将产品 ID 传递给 MyCoolUserControl 时,它不起作用(如果 MyCoolUserControl 上的 ProductID 是 Nullable Int32 - 我可以调试它并且它始终为空)。
我有什么想法可以做到这一点吗?
我做了一个小测试,如果 ProductID 是一个字符串,我就可以正常工作。在我将其更改为用户控件中的 int 后,我遇到了同样的问题。
我在数据源中对中继器执行了 int.Parse 操作,并使其再次工作。
检查您传递到中继器数据源的 ProductId 是否为 int 类型。
我的测试应用程序。
string[] values = new string[]{ "12", "13" };
MyRepeater.DataSource = from v in values
select new
{
ProdId = int.Parse(v)
};
MyRepeater.DataBind();
<asp:Repeater runat="server" ID="MyRepeater">
<ItemTemplate>
<My:control runat="server" ProductId='<%# Eval("ProdId") %>' />
</ItemTemplate>
</asp:Repeater>
并在用户控件中:
public int? ProductId
{
set { MyLabel.Text = value.Value.ToString(); }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)