views:

4291

answers:

1

Through CommandName="Delete" I try to delete a line from the ListView control but not from the datasource. On Pressing Delete I expect the webpage to reload and show me the updated ListView(with one line deleted). But nothing changes, the ListView will display the same content after pressing Delete. What do I do wrong? :

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

And this is the code behind:

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);
    }
}

If I don't use the e.ExceptionHandled = true;, after pressing the Delete link the webpage will come up with a "Specified method is not supported." message. Why? If I use the above mentioned line, then the page refreshes but I can still see all original lines (although on debugging I can see that the ListVieItem collection now onbly contains an item less.)

+1  A: 

It's because of the DatasourceID parameter, which binds at every single postback on the original file.

What you should do is to bind your list on the first page load only. The delete button will work as you expect then.

--- after comments.

OK. In fact, the Delete command would work if you had defined the Delete method on your datasource. Since that's not what you want, you must define the ItemCommand event handler and tell it to remove the ListViewItem that issued the event.

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

It will do so without touching the XML file beneath. Do not databind against it, else the "deleted" row will appear again.

Johan Buret
I took out DataSourceID="XmlDataSource1" from the aspx listview declaration and moved it to the Page_Load: if (!IsPostBack) { DanielsList.DataSourceID = "XmlDataSource1"; DanielsList.DataBind(); }It still the same, after deleting I still see all rows.
Daniel Stanca
Please edit your question, to show the code behind the Delete event.
Johan Buret
there is no code behind. I only need to remove the selected line from the listview. Isn't the aspx code enough for that ?
Daniel Stanca
I brought the suggested code into my question. It still didn't work.
Daniel Stanca
You must also use the "if (!IsPostBack) { DanielsList.DataSourceID = "XmlDataSource1"; DanielsList.DataBind(); } " part
Johan Buret