简体   繁体   中英

Deleting item from ListView

I am trying to do an ASP.NET application. I am using a DataSet which I set as DataSource in a ListView that I have in my .aspx file. The problem is when I click the delete button nothing seems to happen, but when I refresh the browser I get an error message as follows:

"To display the webpage again, the web browser needs to resend the information you've previosly submitted."

I have really tried to figure out what I am doing wrong or what might cause this. This is my C# code:

namespace WebApplication1
{
    public partial class _Default : Page
    {

        DataSet orderDetails;

        protected void Page_Load(object sender, EventArgs e)
        {

            Message.Text = "";

            string orderPath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/orders.xml";
            string orderSchemaPath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/orderschema.xsd";

            XDocument document = XDocument.Load(orderPath);
            XmlSchemaSet schemas = new XmlSchemaSet();
            schemas.Add("", XmlReader.Create(orderSchemaPath));
            bool errors = false;

            document.Validate(schemas, (o, err) =>
            {
                System.Diagnostics.Debug.WriteLine("Validation error: {0}", err.Message);
                errors = true;
            });

            if (!errors)
            {
                System.Diagnostics.Debug.WriteLine("XML document successfully validated.");

                try
                {
                    orderDetails = new DataSet();
                    orderDetails.ReadXml(orderPath);

                    listViewOrders.DataSource = orderDetails;
                    listViewOrders.DataBind();
                }

                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Error: " + ex);
                }

            }
            else
            {
                System.Diagnostics.Debug.WriteLine("XML document does not validate.");
            }
            System.Diagnostics.Debug.WriteLine("Page_load done");
        }

        void BindData()
        {
            listViewOrders.DataSource = orderDetails;
            listViewOrders.DataBind();
        }

        //Call databind method in your prerender event
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void OrderListView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                listViewOrders.DeleteItem(e.Item.DataItemIndex);
                BindData();
            }
        }

        protected void OrdersListView_ItemDeleted(object sender, ListViewDeletedEventArgs e)
        {
            //Check if an exception occurred to display an error message.
            if (e.Exception != null)
            {
                Message.Text = "An exception occurred deleting the contact.";
                e.ExceptionHandled = true;
            }
            else
            {
                // Clear the selected index.
                listViewOrders.SelectedIndex = -1;
            }
        }

        protected void OrdersListView_OnItemDeleting(object sender, ListViewDeleteEventArgs e)
        {


        }

    }
}

And this is the ListView that I have in my .aspx file:

        <asp:ListView ID="listViewOrders"
        DataKeyNames="orderid"
        runat="server"
        OnItemDeleting="OrdersListView_OnItemDeleting"
        OnItemCommand="OrderListView_ItemCommand">
        <LayoutTemplate>
            <table cellpadding="2" width="640px" border="1" runat="server" id="tblProducts">
                <tr runat="server">
                    <th runat="server">Order person</th>
                    <th runat="server">Order ID</th>
                    <th runat="server">Delete</th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr runat="server">
                <td>
                    <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("orderperson") %>' />
                </td>
                <td valign="top">
                    <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("orderid") %>' />
                </td>
                <td>
                    <asp:LinkButton ID="DeleteButton" runat="Server" class="glyphicon glyphicon-remove" CommandName="Delete" CommandArgument="X"  />
                </td>
            </tr>
        </ItemTemplate>
        <EditItemTemplate>
            <tr style="background-color: #ADD8E6">
                <td>
                    <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />&nbsp;
          <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                </td>
                <td>
                    <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%#Bind("orderperson") %>'
                        MaxLength="50" /><br />
                </td>
                <td>
                    <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%#Bind("orderid") %>'
                        MaxLength="50" /><br />
                </td>
            </tr>
        </EditItemTemplate>
    </asp:ListView>

When you are using the listView Commands you have to update the panels.

Place your listView inside a asp:panel like this

 <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
       ... your list View Code
    </ContentTemplate>
 </asp:UpdatePanel>

And after the DataBind you have to update the panel by

UpdatePanel1.Update();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM