ASP.NET的ListView在EditItemTemplate里和InsertTemplate则相同的标记 [英] ASP.NET ListView with identical markup in EditItemTemplate and InsertItemTemplate

查看:770
本文介绍了ASP.NET的ListView在EditItemTemplate里和InsertTemplate则相同的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,其中包括一个EditItemTemplate中和InsertTemplate则。这两种形式分享几乎所有的标记。例如:

I have a ListView that includes an EditItemTemplate and an InsertItemTemplate. The two forms share almost all of their markup. For example:

<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>

当然,在现实中有很多事情在插入和编辑模板(很多领域,具有格式化,验证等),我讨厌要保持相同的标记两次。

Of course, in reality there's a lot going on in the insert and edit templates (lots of fields, with formatting, validation, etc.), and I hate to have to maintain the same markup twice.

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

My first thought was to move all the shared markup to a user control (.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>

不幸的是,双向绑定(文='&LT;%#绑定(富)%>)只能单向当表单是一个用户控件(它不坚持从数据控制返回到数据库)。

Unfortunately, the two-way binding (text='<%#Bind("Foo")%>') only works one way when the form is in a user control (it doesn't persist the data from the controls back to the database).

另一种方法是将所有共享标记移动到一个包含文件。服务器端包含是倒退到传统的ASP,但他们仍然在ASP.NET工作,并能在这样的情况下非常有用,因为包含文件的内容一样处理标记这是正确的页面上。

An alternative would be to move all the shared markup to an include file. Server-side includes are a throwback to classic ASP, but they still work in ASP.NET and can be useful in situations like this, because the contents of the include file are treated just like markup that's right on the page.

但包括文件还是有点做作,并有各自的缺点(例如VisualStudio的是不是用起来很舒服)。是否有其他选择吗?

But include files are still a little hokey, and have their disadvantages (e.g. VisualStudio isn't very comfortable with them). Is there an alternative?

推荐答案

我已经结束了做一个自定义的ListView,使这个简单。如果没有InsertTemplate则,则EditItemTemplate中同时用于:

I've ended up making a custom ListView that makes this straightforward. If there is no InsertItemTemplate, then the EditItemTemplate is used for both:

    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

我也创建了一个自定义的保存按钮切换更新,插入之间的CommandName的适当的:

I've also created a custom "save" button that switches its commandname between "Update" and "Insert" as appropriate:

    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。)

(The GetListView function above just walks up the button's parents until it finds a ListView.)

这就是它 - 希望这是有益的人。

That's it - hope this is helpful to someone.

这篇关于ASP.NET的ListView在EditItemTemplate里和InsertTemplate则相同的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆