C# 控件位于ItemTemplate中时,.Net designer.cs未更新
我有一个简单的表格。比如说一个文本框和一个按钮 如果将这些项目放置在aspx页面上,它们将自动添加到*.designer.cs文件中,并且可以在my C#code behind中引用它们。就像它应该的那样 当我获取这些项并将它们放置在FORMCOVIEW控件的中时,就会出现问题。此时,designer.cs文件中的所有跟踪都将被删除,使用这些控件编写的任何代码现在都会出现“当前上下文中不存在”错误。FormView控件本身留在设计器代码中。如果我再加上它们,它们就粘不住了。如果我删除designer.cs并让它重做,它只会在没有控件的情况下重做C# 控件位于ItemTemplate中时,.Net designer.cs未更新,c#,designer,itemtemplate,C#,Designer,Itemtemplate,我有一个简单的表格。比如说一个文本框和一个按钮 如果将这些项目放置在aspx页面上,它们将自动添加到*.designer.cs文件中,并且可以在my C#code behind中引用它们。就像它应该的那样 当我获取这些项并将它们放置在FORMCOVIEW控件的中时,就会出现问题。此时,designer.cs文件中的所有跟踪都将被删除,使用这些控件编写的任何代码现在都会出现“当前上下文中不存在”错误。FormView控件本身留在设计器代码中。如果我再加上它们,它们就粘不住了。如果我删除design
有什么线索吗?如果要向FormView添加控件,请使用所需的控件,然后将其放到itemTemplate上。然后您可以从代码隐藏中访问这些控件 这是一个样本
<asp:FormView >
<ItemTemplate id="MyControl" runat="server">
<asp:linkbutton id="Edit" text="Edit"
commandname="Edit" runat="server"/>
<asp:textbox id="FirstNameTextBox"
text='<%# Bind("FirstName") %>'
</ItemTemplate>
</asp:FormView>
下面是一篇很好的文章,可以让你继续前进
希望这有帮助我通过稍微不同的方式寻找答案,找到了答案。您必须使用FindControl,因为这些项位于FormView控件中。见示例:
posting.Title=((文本框)FormView1.FindControl(“txtTitle”)).Text 如果您只处理1个EditItemTemplate(或任何一个模板),则另一种方法是从FormView继承并覆盖设置为。像这样:
public class FormView : System.Web.UI.WebControls.FormView
{
[Browsable(false),
DefaultValue((string)null),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(FormView), BindingDirection.TwoWay),
TemplateInstance(TemplateInstance.Single)]
public override ITemplate EditItemTemplate
{
get { return base.EditItemTemplate; }
set { base.EditItemTemplate = value; }
}
}
如果在页面中使用该FormView控件,EditItemTemplate中的控件将显示在设计器中,并且在代码隐藏中也可以直接访问。感谢您的快速回答。但是,我确实在问题中提到了这一部分,但它去掉了,因为它没有标记为代码。您是否在模板字段中放置了控件?ItemTemplate没有“id”或“runat”属性。
public class FormView : System.Web.UI.WebControls.FormView
{
[Browsable(false),
DefaultValue((string)null),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(FormView), BindingDirection.TwoWay),
TemplateInstance(TemplateInstance.Single)]
public override ITemplate EditItemTemplate
{
get { return base.EditItemTemplate; }
set { base.EditItemTemplate = value; }
}
}