简体   繁体   English

从ItemTemplate复制控件

[英]Copy controls from ItemTemplate

I'm dealing with FormView . 我正在处理FormView
I have a ridiculous long input form, so i was wondering if there's a way to programmatically copy ALL the controls from ItemTemplate to InsertItemTemplate/UpdateItemTemplate so I don't have to repeat the all Text Boxes / DDLs etc in the .aspx since the information entered is the same for both Update /Insert (I already know I can copy /paste, but the .aspx is messy enough already). 我有一个荒谬的长输入格式,所以我想知道是否有一种方法可以通过编程方式将所有控件从ItemTemplate复制到InsertItemTemplate/UpdateItemTemplate所以我不必在.aspx重复所有文本框/ DDL等。输入的两个Update / Insert相同(我已经知道我可以复制/ paste,但是.aspx已经足够混乱了)。

I am able to set InsertItemTemplate = ItemTemplate and when I set the mode to Insert the fields are all displayed. 我可以设置InsertItemTemplate = ItemTemplate并且当我将模式设置为Insert时,将显示所有字段。 The problem is that when I try to do a FindControl in the submit event I always get null. 问题是,当我尝试在FindControl事件中执行FindControl ,我总是得到null。

If you put all your controls in ItemTemplate into a container - say a panel - you can get to them in code through that panels .Controls property. 如果将ItemTemplate中的所有控件放入一个容器(例如一个面板)中,则可以通过该面板的.Controls属性以代码形式获取它们。 Iterating over this you could then add them to panels in the other templates... Just an idea... :) 对此进行迭代,然后可以将它们添加到其他模板的面板中...只是一个想法... :)

foreach (var item in myItemPanel.controls) myInsertPanel.controls.add(item) foreach(myItemPanel.controls中的可变项)myInsertPanel.controls.add(item)

If you have nested the original controls inside each others, you'd need to reflect that in the foreach loop... 如果您将原始控件相互嵌套,则需要在foreach循环中反映出来...

You can create a user control to hold your layout. 您可以创建一个用户控件来保存您的布局。 Register the control in your webpage: 在您的网页中注册控件:

<%@ Register TagPrefix="ctrl" TagName="FormControl" Src="FormControl.ascx" %>

And add it to the FormView : 并将其添加到FormView

<asp:FormView ID="FormView1" runat="server" 
DataSourceID="ObjectDataSource1" 
AllowPaging="True" EnableViewState="False">

  <ItemTemplate>
    <ctrl:FormControlID="MyControl1" runat="server" Mode="Item"/>
  </ItemTemplate>
  <InsertItemTemplate>
    <ctrl:FormControlID="MyControl2" runat="server" Mode="InsertItem"/>
  </InsertItemTemplate>
</FormView>

I added a Mode argument because it could be to change the layout inside your control depending on the selected mode. 我添加了Mode参数,因为它可能会根据所选模式更改控件内部的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM