ASP.NET: bind value to user control inside relay

I have an ASP.NET control that binds data to a relay. Inside this repeater, I have another user control. I want to pass a value to this second control based on the current binding element.

<asp:Repeater runat="server" ID="ProductList">
    <ItemTemplate>
        <p>Product ID: <%# Eval("ProductID") %></p>
        <myControl:MyCoolUserControl runat="server" ProductID='<%# Eval("ProductID") %>' />
    </ItemTemplate>
</asp:Repeater>

The repeater element template correctly prints my product identifier using the Eval operator, but when I do the same to pass the product identifier to MyCoolUserControl, it does not work (if the ProductID in MyCoolUserControl is Nullable Int32 - I can debug it and it always matters null).

Any ideas how I can do this?

+3
source share
1 answer

I did a little test and I got it if ProductID is a string. After I changed it to int and in intcontrol, I had the same problems. I did int.Parse in the data source for the repeater and started working again. Make sure that the ProductId that you pass to the repeater data source is int.

Mytest app.

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>

and in usercontrol:

public int? ProductId
{
    set { MyLabel.Text = value.Value.ToString(); }
}
+3
source

Source: https://habr.com/ru/post/1710228/


All Articles