【问题标题】:Deleting item from ListView从 ListView 中删除项目
【发布时间】:2017-03-15 18:33:16
【问题描述】:

我正在尝试做一个 ASP.NET 应用程序。我正在使用我在 .aspx 文件中的 ListView 中设置为 DataSource 的 DataSet。问题是当我单击删除按钮时似乎没有任何反应,但是当我刷新浏览器时,我收到如下错误消息:

"要重新显示网页,网络浏览器需要重新发送 您之前提交的信息。”

我真的试图弄清楚我做错了什么或可能导致这种情况的原因。这是我的 C# 代码:

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


        }

    }
}

这是我的 .aspx 文件中的 ListView:

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

【问题讨论】:

  • 我注意到你的加载绑定,你试过检查它是否是回发吗?
  • @Trey 实际上我没有......实际上,当我添加一个 if(isPostback) {} 时,基本上 Page_Load 中的所有内容都被包裹住了,消息停止了。至少大多数时候。如果我不断刷新页面,它会在 3-4 次后弹出。现在我有一个不同的问题,那就是我在 ListView 中的所有项目都消失了。无论如何,你认为在 Page_Load 中绑定不好吗?
  • 不错不错...据说我会放入一个您在页面加载等时调用的函数中。那么您现在有什么问题?如果可能,发布错误并找到它。
  • @Trey 非常感谢您花时间提供帮助。除了在 Page_Load 中检查 isPostback,我添加了 Response.Redirect(Request.Url.PathAndQuery);在 OrderListView_ItemCommand 函数的末尾。但现在似乎什么都没有发生(至少我在浏览器中看到的)。我的意思是最终目标是从 XML 中删除一个项目,但最初我希望它从 DataSource 中删除(如您所见,这是一个 DataSet)。如果我使用更新的代码和到目前为止的进度来编辑我的原始帖子会有帮助吗?

标签: c# asp.net listview


【解决方案1】:

当您使用 listView 命令时,您必须更新面板。

将您的 listView 放在这样的 asp:panel 中

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

在 DataBind 之后,您必须通过

更新面板
UpdatePanel1.Update();

【讨论】:

  • 我认为这并不能真正解决问题。通常,在将 AJAX 添加到组合之前,您希望一切正常。
  • @Sailor 我很抱歉说它没有帮助,但如果这与 Seano666 所说的 AJAX 有关,我稍后一定会看看 UpdatePanel,所以谢谢!
  • 您的装订在哪里完成的?你能分辨出哪个函数首先击中了 OnItemDeleting 吗?项目删除?还是 ItemCommand?
  • @Sailor 我在 C# 代码中使用 listViewOrders.DataBind() 在 Page_Load 中进行了绑定。然后,当按下 Delete 按钮时,似乎(根据我现在制作的一些打印输出)我首先到达 OnItemCommand,然后是 OnItemDeleting,然后它永远不会到达 OnItemDeleted,因为它似乎直接调用了 Page_load。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多