【发布时间】:2012-05-09 10:27:46
【问题描述】:
我有一个 UserControl,其中有一个 FormView。
表单视图只有一个 InsertItemTemplate(我不需要其他任何东西)
<irt:FormView ID="FormViewInsertEvent" DefaultMode="Insert" runat="server" DataKeyNames="EVENT_ID"
DataSourceID="SqlDataSourceIocEvents">
<InsertItemTemplate>
//Some form elements (text boxes and labels etc.) which are common
<%if (CustomContent != null)
{ %>
<hr />
<asp:PlaceHolder runat="server" ID="PlaceHolderCustomContent"></asp:PlaceHolder>
<%} %>
// Link buttons with insert command
</InsertItemTemplate>
</irt:FormView>
后面的代码是这样的:
public partial class EventControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (CustomContent != null)
{
Control ph = FormViewInsertEvent.FindControl("PlaceHolderCustomContent");
CustomContent.InstantiateIn(ph);
}
}
[
DefaultValue(null),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateInstance(TemplateInstance.Single),
Browsable(false)
]
public ITemplate CustomContent {
get;
set;
}
}
在调用者(页面)中我有这样的东西(我将数据源传递给 UC 并从后面的代码中设置 FormView 的数据源。这没有问题):
<irt:EventControl ID="EventControl" runat="server" DataSourceID="SqlDataSourceIocEvents">
<CustomContent>
Custom Field:
<asp:TextBox ID="TextBoxCustomField" runat="server" Text='<%# Bind("CustomField") %>' />
</CustomContent>
</irt:EventControl>
我的问题是;当我单击链接按钮和 PostBack 时,自定义内容(即我放在模板字段中的内容)消失了。
如果我将 asp:PlaceHolder 放在 FormView.InsertItemTemplate 之外,则没有问题。但这不是我需要的。
即使在回发之后,我也需要在 InsertItemTemplate 中保留 ITemplate。看起来我的模板已添加到 PlaceHolder 的控件列表中,但在 PreRender 和 Render 之间的某个位置,这些控件被删除了。
有什么想法吗?
谢谢 南墩
【问题讨论】:
-
您是否解决了这个问题以及如何解决的?我遇到了完全相同的问题。杰森
-
嗨@JasonV 我解决了这个问题,但我的一生都记不清我到底做了什么。我从尘土飞扬的架子上拿出代码并仔细阅读,我认为是一些类和属性属性最终解决了这个问题。我会把它作为答案,如果它适合你,请将它标记为答案。
标签: c# asp.net formview itemplate