.NET 4.5 WebForms:我(仍然)真的必须在 FormView 中指定

.NET 4.5 WebForms: do I (still) really have to specify all 3 templates in a FormView?(.NET 4.5 WebForms:我(仍然)真的必须在 FormView 中指定所有 3 个模板吗?)

本文介绍了.NET 4.5 WebForms:我(仍然)真的必须在 FormView 中指定所有 3 个模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

研究 ASP.NET 4.5 WebForms 中新的强类型模型绑定方法:

Investigating the new strongly-typed, model-binding approach within ASP.NET 4.5 WebForms:

在 Scott Hanselman 的示例中我见过的 WebForms 模型绑定(以及其他)使用在编辑"模式下打开的 FormView,其中包含许多 DynamicControls 例如

In Scott Hanselman's example of WebForms model binding (amongst others) I've seen the use of a FormView that opens in "Edit" mode, containing a number of DynamicControls e.g.

<asp:FormView runat="server" ID="MyForm" ... DefaultMode="Edit">
  <EditItemTemplate>
    <asp:DynamicControl runat="server" ID="Field1" DataField="Field1" Mode="Edit" />
    <asp:DynamicControl runat="server" ID="Field2" DataField="Field2" Mode="Edit" />
  </EditItemTemplate>
</asp:FormView> 

在我的情况下,我的 FormView 的 ItemTemplateEditItemTemplateInsertItemTemplate 将是相同的,除了 ItemTemplate 的控件将处于只读"模式.

In my situation, my FormView's ItemTemplate, EditItemTemplate and InsertItemTemplate will be identical, except the ItemTemplate's controls will be in "ReadOnly" mode.

我(仍然)真的需要在 FormView 中提供三个几乎相同的模板副本吗?

Do I (still) really need to provide three near-identical copies of the template within the FormView?

我很高兴使用 DynamicControls,但这里的团队永远不会采用 FormView 似乎需要的3x 复制粘贴"方法,尤其是对于我们的大型模板.

I'm happy to use DynamicControls, but the team here will never go for the "3x copy-paste" approach seemingly required for the FormView, especially for our large templates.

我原以为:

  • DynamicControl 是否可以从包含的 FormView 中获取它们的模式"?
  • 我可以使用 FormView 以外的其他东西来包含我的 DynamicControls 吗?
  • 我是否应该在代码隐藏中管理 DynamicControls 的模式以避免模板重复?

有什么例子/想法吗?

推荐答案

与许多人认为的相反,您只需要 一个 FormView 中的模板,即 EditItemTemplate.

Contrary to what many believe, you need only one template in a FormView, the EditItemTemplate.

下面是一个简单的例子,展示了如何做到这一点(注意这与动态数据"的想法无关.).

Below is a simple example showing how to do it (notice that this is not connected to the idea of "Dynamic Data".).

通过这种方式,从不使用 ReadOnly 模式,因此不需要 ItemTemplate.FormView 将使用 EditItemTemplate 进行编辑和插入.

In this way of doing it, the ReadOnly mode is never used, and thus an ItemTemplate is not needed. And the FormView will use the EditItemTemplate for both editing and inserting.

这种做法大大简化了标记,当您对布局进行调整时,只需在一个模板中进行即可.

This way to do it simplifies the markup a lot, and when you make adjustments to the layout, you only have to do it in the one single template.

请注意,保存按钮没有 CommandName.该命令是在 FormView1_ItemCommand 事件中确定的(参见代码).

Notice that the save-button has no CommandName. The command is instead determined in the FormView1_ItemCommand event (see code).

另请注意,FormView 的模式是在 SqlDataSource1_Selected 事件中确定的(请参阅该代码,带有注释).

Also notice that the mode of the FormView is determined in the event SqlDataSource1_Selected (see that code, with comments).

我没有包含 SqlDataSource1 的标记,因为对于那个标记,您不需要考虑什么特别的.照常做就好了.

I have not included the markup for the SqlDataSource1, because there is nothing special you need to think about for that one. Just make it as usual.

    <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
      DataKeyNames="ApplicationId,UserId"
    >
      <EditItemTemplate>
        <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("firstName") %>'></asp:TextBox><br />
        <asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("age") %>'></asp:TextBox><br />
        <asp:Button ID="btnSave" runat="server" Text="Save" />  
      </EditItemTemplate>
    </asp:FormView>

  Private Sub FormView1_ItemCommand(sender As Object, e As FormViewCommandEventArgs) Handles FormView1.ItemCommand
    Select Case FormView1.CurrentMode
      Case FormViewMode.Edit
        FormView1.UpdateItem(True)
      Case FormViewMode.Insert
        FormView1.InsertItem(True)
    End Select
  End Sub

  Private Sub SqlDataSource1_Selected(sender As Object, e As SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
    If e.AffectedRows = 0 Then
      ' nothing exists yet, so make formview ready to insert
      FormView1.ChangeMode(FormViewMode.Insert)
    Else
      ' something exists already, so make formview ready to edit
      FormView1.ChangeMode(FormViewMode.Edit)
    End If
  End Sub

这篇关于.NET 4.5 WebForms:我(仍然)真的必须在 FormView 中指定所有 3 个模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:.NET 4.5 WebForms:我(仍然)真的必须在 FormView 中指定

基础教程推荐