在detailview中添加动态templatefield? [英] add dynamic templatefield in detailview ?
本文介绍了在detailview中添加动态templatefield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
如何在asp.net c#的detailview中添加动态templatefield?请建议我可以.
谢谢.
Hello,
How to add dynamic templatefield in detailview of asp.net c# ? Please suggest me I can it.
Thanks.
推荐答案
您好,在您的代码隐藏中,将数据绑定到DatailsView之后,您可以创建一个新的模板字段,然后将其添加到DetailsView中:
Hello, in your code-behind, after the databind to you DatailsView you can create a new template field, then add it to the DetailsView:
TemplateField TheTF = new TemplateField();
TheDetailsView.Fields.Add(TheTF);
TheTF.ItemTemplate = AddTemplate(ListItemType.Item);
//Rebind after creation of itemtemplate.
TheDetailsView.DataBind()
您不能直接在TemplateFields中添加项目,例如,您需要创建一个模板(该模板继承自ITemplate):
You can''t add items directly in the TemplateFields, se you need to create a template (That inherits from ITemplate):
public class AddTemplate : ITemplate
{
private ListItemType m_ListItemType;
public AddTemplate(ListItemType _listItemType)
{
m_ListItemType = _listItemType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
if (m_ListItemType == ListItemType.Item)
{
TextBox TheIDTb = new TextBox();
TheIDTb.DataBinding += new EventHandler(TheIDTb_DataBinding);
container.Controls.Add(TheIDTb);
}
}
void TheIDTb_DataBinding(object sender, EventArgs e)
{
TextBox TheIDTb = (TextBox)sender;
string str = TheIDTb.NamingContainer.ToString();
DetailsView container = (DetailsView)TheIDTb.NamingContainer;
TheIDTb.Text = ((DataRowView)container.DataItem)["id"].ToString();
}
}
这篇关于在detailview中添加动态templatefield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文