使用绑定动态添加文本框

时间:2014-05-25 01:49:21

标签: c# asp.net

我正在向我的DataList ItemDataBound事件动态添加一个TextBox控件。如何以编程方式将text属性绑定到我的对象?

void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Panel p1 = e.Item.FindControl("Panel1") as Panel;
        TextBox t1 = new TextBox();
        p1.Controls.Add(t1);    
        //need to add something like <%# Bind("MyProperty") %>    
    }
}

1 个答案:

答案 0 :(得分:0)

TextBox t1 = new TextBox();
t1.text =((YourClass)e.Item.DataItem).YourProperty
p1.Controls.Add(t1);