locked
e.Values NULL in ListView_ItemDeleting Event RRS feed

  • Question

  • User-253199000 posted

    Hi,

    I raise the ListView_ItemDeleting vent in order to do some pre-deleting operations before the actual item is deleted from the db.

    Thought I could access all props from the item via the ListViewDeleteEventArgs e.Values collection, but it is empty. The PK is available through the e.Keys collection, but all other fields which I assumed to be in the e.Values collection are non-existant.

    Can anybody confirm this or has any advice?

    Thanks, Stephan

    Friday, November 30, 2018 5:15 PM

All replies

  • User-1716253493 posted

    AFAIK, Delete parameters come from DataKeys only. Do you have multiple datakeys?

    Friday, November 30, 2018 11:27 PM
  • User-893317190 posted

    Hi steschu,

    Maybe you are using Eval instead of Bind, you could try to use bind.

    <ItemTemplate>
                         <tr>
                            
                              <td><asp:Label ID="dincome" runat="server" Text=' <%# Bind("name") %>'></asp:Label></td>
                               <td><asp:Label ID="Label1" runat="server" Text=' <%# Bind("sid") %>'></asp:Label></td>
                               <td><asp:Label ID="Label2" runat="server" Text=' <%# Bind("city") %>'></asp:Label></td>
                               <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /></td>
                         </tr>
                        
                     </ItemTemplate>

    Best regards,

    Ackelry Xu

    Monday, December 3, 2018 7:34 AM
  • User-253199000 posted

    I used Bind instead but still e.Values is not populated:

    Friday, December 7, 2018 9:03 PM
  • User-1716253493 posted

    AFAIK, Bind only for editing and inserting. Not sure if you can bind itemtemplate for deleting

    If you can't pass the values, you can manualy set it. Set e.values from current gridviewrow values based rowindex

    Saturday, December 8, 2018 5:33 AM
  • User-1716253493 posted

    Use Multiple Keys

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="Key1,Key2,Key3">
    
    
    
    </asp:ListView>
        protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            string key1 = e.Values["key1"].ToString();
    
        }

    Or

        protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            int idx = e.ItemIndex;
            string key2 = ((HiddenField)ListView1.Items[idx].FindControl("HiddenField1")).Value;
            e.Values["key2"] = key2;
        }

    Saturday, December 8, 2018 2:07 PM
  • User-893317190 posted

    Hi steschu,

    You should use e.values.

    Best regards.

    Ackerly Xu

    Monday, December 10, 2018 1:23 AM
  • User-253199000 posted

    I don't know why e.Values is populated in your example, as I stated above in mine it is not, e.Values count is zero?! That's strange. Maybe it depends on something else...

    I think the best solution is to get the data accessing the row via the e.ItemIndex. Using multiple keys in my opinion is not a good solution.

    Monday, December 10, 2018 7:26 AM
  • User-893317190 posted

    Hi steschu,

    I have tried to changed my datasource to datatable instead of sqldatasource , but still could get the values.

    If you could not get values from e.values, you could get the value from findcontrol method,only if you write eval in a contral for example in a label.

    Below is my whole code.

       <asp:ListView ID="ListView_Budget" runat="server" DataKeyNames="sid" ItemPlaceholderID="itemPlaceholder" OnDataBound="ListView1_DataBound" OnItemDeleting="ListView_Budget_ItemDeleting">
                     <LayoutTemplate   >
                         <table   class="table table-bordered">
    
                              <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                         </table>
                   
    
                     </LayoutTemplate>
                     <ItemTemplate>
                         <tr>
                                              <!--if you use findcontrol to get values , you could change Bind to Eval, only if it is the value of a control-->
                              <td><asp:Label ID="dincome" runat="server" Text=' <%# Bind("name") %>'></asp:Label></td>
                               <td><asp:Label ID="Label1" runat="server" Text=' <%# Bind("sid") %>'></asp:Label></td>
                               <td><asp:Label ID="Label2" runat="server" Text='<%# Eval("city") %> '></asp:Label></td>
                               <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" /></td>
                         </tr>
                        
                     </ItemTemplate>
                    
                 </asp:ListView>
                <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
    
               
            </div>

    Code behind.

    private static string constr = ConfigurationManager.ConnectionStrings["WebFormCases.Models.supplyModelConnectionString"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [Suppliers]", constr))
                    {
    
                       
                        DataTable table = new DataTable();
                        adapter.Fill(table);
                        ListView_Budget.DataSource = table;
                        ListView_Budget.DataBind();
                    }
                }
            }
            
           
    
            protected void ListView_Budget_ItemDeleting(object sender, ListViewDeleteEventArgs e)
            {
                Label lable = ListView_Budget.Items[e.ItemIndex].FindControl("Label2") as Label;   // get the original value from the label
               IOrderedDictionary keyValuePairs = e.Values;
            }

    Best regards,

    Ackerly Xu

    Tuesday, December 11, 2018 1:18 AM