2008-12-01 156 views
4

通過CommandName="Delete"我嘗試從ListView控件中刪除一行,而不是從數據源刪除一行。按刪除我希望網頁重新加載並向我顯示更新的ListView(刪除一行)。但沒有任何更改,按下Delete後,ListView將顯示相同的內容。我做錯了什麼?Asp.Net ListView如何刪除一行而不從數據源中刪除

<asp:ListView ID="ListView1"  
        DataSourceID="XmlDataSource1" 
        ItemContainerId="DataSection"      
        runat="server">   
    <LayoutTemplate> 
    <h3>Protocols to Upload...</h3>        
     <table border=0 style="background-color:#9C9EFF; width: 100%;"> 
     <tr align=left> 
      <th>Region/Exam/Program</th><th>Protocol</th><th>Position</th> 
     </tr>      
     <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder> 
     </table> 
    </LayoutTemplate>        
    <ItemTemplate>   
     <tr> 
     <td><%#XPath("Location/Path")%></td> 
     <td><%#XPath("Location/Name")%></td> 
     <td><%#XPath("Location/Position")%></td> 
     <td style="width:40px"> 
     <asp:LinkButton ID="SelectCategoryButton" runat="server" Text="Select" CommandName="Select"/> 
     </td> 
     </tr> 

    </ItemTemplate>  
    <SelectedItemTemplate> 
     <tr id="Tr1" runat="server" style="background-color:#F7F3FF"> 
     <td><%#XPath("Location/Path")%></td> 
     <td><%#XPath("Location/Name")%></td> 
     <td><%#XPath("Location/Position")%></td> 
     <td style="width:40px"> 
      <asp:LinkButton runat="server" ID="SelectCategoryButton" Text="Delete" CommandName="Delete" /> 
     </td> 
     </tr> 
    </SelectedItemTemplate> 
    <%-- <ItemSeparatorTemplate> 
     <div style="height: 0px;border-top:dashed 1px #ff0000"></div> 
    </ItemSeparatorTemplate>--%> 
    </asp:ListView>   
    <asp:XmlDataSource ID="XmlDataSource1" XPath="HttpRequestBO/ProtocolsDTO/ProtocolDTO" runat="server" 
     DataFile="~/HttpRequestBo.Sample.xml"></asp:XmlDataSource> 

這是後面的代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
} 

protected void ListView1_OnItemDeleted(Object sender, ListViewDeletedEventArgs e) 
{ 
    if (e.Exception != null) 
    {     
     e.ExceptionHandled = true; 
    } 
} 

protected void ListView1_OnItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    if (String.Equals(e.CommandName, "Delete")) 
    { 
     ListViewDataItem dataItem = (ListViewDataItem)e.Item; 
     ListView1.Items.Remove(dataItem); 
    } 
} 

如果我不使用e.ExceptionHandled = true;,按下Delete鏈接後,網頁將拿出一個「指定的方法不支持。 「信息。爲什麼?

如果我使用上述行,然後刷新頁面,但我仍然可以看到所有原始行(雖然在調試,我可以看到ListVieItem集合現在只包含項目少。)

+0

這個問題解決了嗎? – StuperUser 2009-10-22 09:26:32

回答

3

這是因爲DatasourceID參數,它在原始文件上的每一次回發中綁定。

你應該做的是在第一頁加載時綁定你的列表。刪除按鈕將按照您的預期工作。

---評論後。

好的。 事實上,如果您在數據源中定義了Delete方法,那麼Delete命令將起作用。由於這不是你想要的,你必須定義ItemCommand事件處理程序 並告訴它刪除發出該事件的ListViewItem。

protected void yourListView_OnItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    if (String.Equals(e.CommandName, "Delete")) 
    { 
    ListViewDataItem dataItem = (ListViewDataItem)e.Item; 
    yourListView.Items.Remove(dataItem); 
    } 
} 

它會這樣做,而不會觸及下面的XML文件。不要對它進行數據綁定,否則「已刪除」行將再次出現。

+0

我從aspx listview聲明中取出DataSourceID =「XmlDataSource1」並將其移至 Page_Load:if(!IsPostBack) { DanielsList.DataSourceID =「XmlDataSource1」; DanielsList.DataBind(); } 它仍然是一樣的,刪除後我仍然可以看到所有的行。 – 2008-12-01 13:10:49

相關問題