使用用户控件的转发器

时间:2010-09-02 12:19:02

标签: c# asp.net asp.net-2.0 repeater

我有一个转发器,我想用它来多次显示用户控件,填充数据 目前,我有这个:

<asp:Repeater runat="server" ID="MyRepeater" >
    <ItemTemplate>
        <uc1:MyItems ID="MyItems1" runat="server"  />
    </ItemTemplate>
</asp:Repeater>

我的用户控件有三个属性,我想为每个属性填充。我目前有这个:

protected void Page_Load(object sender, EventArgs e)
{
    MyDataSource.SelectCommand =
                "SELECT Name, Address, Phone " +
                "FROM TestTable ";
    MyDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
    DataView resultsdv = (DataView)MyDataSource.Select(DataSourceSelectArguments.Empty);
    foreach (DataRow dr in resultsdv.Table.Rows)
    {            
        MyItems1.Cust_Name = dr["Name"].ToString();
        MyItems1.Cust_Address = dr["Address"].ToString();
        MyItems1.Cust_Tel = dr["Phone"].ToString();
    }
}

显然,这不是我想要的。有可能告诉转发器我想要填充我的用户控件 - 通过数据绑定它,还是以类似于上面的方式手动填充它?

2 个答案:

答案 0 :(得分:3)

<asp:Repeater runat="server" ID="MyRepeater" >
    <ItemTemplate>
        <uc1:MyItems ID="MyItems1" MyItems="<%# Eval("Name") %>" ... runat="server"  />
    </ItemTemplate>
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
    MyDataSource.SelectCommand =
                "SELECT Name, Address, Phone " +
                "FROM TestTable ";
    MyDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
    DataView resultsdv = (DataView)MyDataSource.Select(DataSourceSelectArguments.Empty);
    MyRepeater.DataSource = resultsdv.Table.Rows;
    MyRepeater.DataBind()

}

答案 1 :(得分:1)

我认为这需要一些更正:

<asp:Repeater runat="server" ID="MyRepeater" >
    <ItemTemplate>
        <uc1:MyItems ID="MyItems1" Cust_Name=<%#Eval("Name") %> Cust_Address=<%#Eval("Address")%> Cust_Tel=<%#Eval("Phone")%> runat="server"  />
    </ItemTemplate>
</asp:Repeater>