代码之家  ›  专栏  ›  技术社区  ›  Herb Caudill

在EditItemTemplate和InsertItemTemplate中具有相同标记的ASP.NET ListView

  •  1
  • Herb Caudill  · 技术社区  · 15 年前

    我有一个包含EditItemTemplate和InsertItemTemplate的ListView。这两个表单共享几乎所有的标记。例如:

    <asp:listview runat="server" ... >
       <layouttemplate>...</layouttemplate>
       <itemtemplate>
          <p><%#Eval("Name")%></p>
          <p><%#Eval("Title")%></p>
           ...
       </itemtemplate>
       <insertitemtemplate>
          <p>Name: <asp:textbox runat=server text='<%#Bind("Name")%>' /></p>
          <p>Title: <asp:textbox runat=server text='<%#Bind("Title")%>' /></p>
          ...
          <asp:button runat=server commandname="Insert" text="Save" />
       </insertitemtemplate>
       <edititemtemplate>
          <p>Name: <asp:textbox runat=server text='<%#Bind("Name")%>' /></p>
          <p>Title: <asp:textbox runat=server text='<%#Bind("Title")%>' /></p>
          ...
          <asp:button runat=server commandname="Update" text="Save" />
       </edititemtemplate>
    </asp:listview>
    

    当然,在现实中,插入和编辑模板中会发生很多事情(很多字段,带有格式、验证等),我讨厌两次维护相同的标记。

    我的第一个想法是将所有共享标记移动到用户控件(.ascx):

       <insertitemtemplate>
          <custom:myform runat=server />
          <asp:button runat=server commandname="Insert" text="Save" />
       </insertitemtemplate>
       <edititemtemplate>
          <custom:myform runat=server />
          <asp:button runat=server commandname="Update" text="Save" />
       </edititemtemplate>
    

    不幸的是,双向绑定(text='<%#绑定(“Foo”)%>)当窗体位于用户控件中时,只有一种方式有效(它不会将控件中的数据持久化回数据库)。

    另一种方法是将所有共享标记移动到包含文件中。服务器端包含是对经典ASP的回溯,但它们仍然在ASP.NET中工作,在这种情况下非常有用,因为包含文件的内容就像页面上的标记一样处理。

    但是include文件仍然有点做作,并且有其缺点(例如VisualStudio对它们不太熟悉)。还有别的选择吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Herb Caudill    15 年前

    我最终制作了一个定制的ListView,使这一点变得简单明了。如果没有InsertItemTemplate,则EditItemTemplate将用于以下两种情况:

        Private Sub ListView_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            If Me.InsertItemTemplate Is Nothing Then
                Me.InsertItemTemplate = Me.EditItemTemplate
            End If
        End Sub
    

    我还创建了一个自定义的“保存”按钮,可以根据需要在“更新”和“插入”之间切换命令名:

        Private Sub SaveLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            Dim _ListView As Controls.ListView = GetListView()
            If _ListView IsNot Nothing Then
                If Me.BindingContainer Is _ListView.EditItem Then
                    Me.CommandName = "Update"
                Else
                    Me.CommandName = "Insert"
                End If
            End If
        End Sub
    

    (修订) GetListView 上面的函数只是沿着按钮的父级走,直到找到ListView。)

        2
  •  2
  •   w5l    9 年前

    control FormView ):

    if (control.EditItemTemplate == null)
    {
        control.EditItemTemplate = control.InsertItemTemplate;
    }
    

    <InsertItemTemplate>
    
        ... template ...
    
        <asp:LinkButton Text="Insert" CommandName="Insert" runat="server" 
                        Visible='<%# Container.ItemType == ListViewItemType.InsertItem %>' />
        <asp:LinkButton Text="Update" CommandName="Update" runat="server"
                        Visible='<%# Container.ItemType == ListViewItemType.DataItem %>' />
        <asp:LinkButton Text="Cancel" CommandName="Cancel" runat="server" 
                        Visible='<%# Container.ItemType == ListViewItemType.DataItem %>' />
    </InsertItemTemplate>
    

    Container.ItemType == ListViewItemType.DataItem (和其他)。这将根据模板类型正确设置按钮的可见性。