2010-02-02 35 views
1

我有一個包含EditItemTemplate和InsertItemTemplate的ListView。這兩種形式幾乎分享了所有的標記。例如:ASP.NET ListView在EditItemTemplate和InsertItemTemplate中具有相同的標記

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

不幸的是,雙向綁定(文=」 <%#綁定(‘富’) %>')僅在窗體位於用戶控件中時才起作用(它不會將控件中的數據持久保存到數據庫中)。

另一種方法是將所有共享標記移動到包含文件。服務器端包含是對傳統ASP的一種迴歸,但它們仍然可以在ASP.NET中使用,並且可以在這種情況下使用,因爲包含文件的內容被視爲與頁面上正確的標記一樣。

但是包含文件仍然有點混亂,並且有它們的缺點(例如VisualStudio對它們不太舒服)。有其他選擇嗎?

+0

根據這篇文章:'http:// forums.asp.net/p/1344635/2729853.aspx',這是非常棘手的做它聲明。從代碼隱藏中創建模板會更容易。 – keyboardP 2010-02-02 14:43:12

回答

2

我已經做了一個自定義的ListView,使得這個直截了當。如果沒有InsertTemplate則,則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。)

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

1

我非常遲到了,但爲尋找一個解決方案的聲明,最後我做了以下(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(和其他人)。這可以根據模板類型正確設置按鈕的可見性。

相關問題