ASP.NET:绑定一个中继器内的值设置为自定义用户控件 [英] ASP.NET: Bind a value to a custom user control inside a repeater

查看:74
本文介绍了ASP.NET:绑定一个中继器内的值设置为自定义用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有绑定数据中继ASP.NET控件。里面的那个中继器,我有另外一个自定义用户控件。我想传递一个值基于当前绑定的项目本次控制。

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

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

转发器项目模板正确地打印出我的产品ID与eval语句,但是当我做同样的事情要通过产品ID来MyCoolUserControl,这是行不通的(如果产品ID上MyCoolUserControl是一个空Int32 - 我可以调试它,它总是为空)。

The repeater item template correctly prints out my Product ID with the Eval statement, but when I do the same thing to pass the Product ID to MyCoolUserControl, it doesn't work (if ProductID on MyCoolUserControl is a Nullable Int32 - I can debug it and it's always null).

任何想法我怎么可以这样?

Any ideas how I can do this?

推荐答案

我做了一个小测试,我得到了它的工作,如果产品ID是一个字符串。之后,我把它改成和int在该用户我得到了一种同样的问题。
我确实在数据源的中继器的int.Parse得到它再次合作。
请检查您传递到中继器的数据源的产品编号是int类型的。

I did a small test and I got it working if the ProductID is a string. After I changed it to and int in the usercontrol I got kind of the same problems. I did a int.Parse in the datasource to the repeater and got it working again. Check to see that the ProductId that you pass into the repeaters datasource is of type int.

MYTEST应用程序。

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>

和在该用户:

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

这篇关于ASP.NET:绑定一个中继器内的值设置为自定义用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆