【问题标题】:C# = The ListView raised event which wasn't handled.C# = 未处理的 ListView 引发的事件。
【发布时间】:2017-03-27 01:08:34
【问题描述】:

有一个 ListView 显示从数据库中检索到的数据。

我想通过单击旁边的编辑按钮来编辑 ListView 上的单个记录,但如果我按下编辑按钮,我会收到错误消息:

The ListView 'lvItemView' raised event ItemEditing which wasn't handled. 

这是列表视图:

<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder">
        <LayoutTemplate>
            <table style="color: Black;" width="100%" border="0" cellpadding="5">
                <tr>
                    <th style="text-align: center;">Customer ID</th>
                    <th style="text-align: center;">Contact Name</th>
                    <th style="text-align: center;">Company</th>
                    <th style="text-align: center;">Created By</th>
                    <th style="text-align: center;">Created Date</th>
                    <th style="text-align: center;">Modified By</th>
                    <th style="text-align: center;">Modified Date</th>
                </tr>
                <asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td align="left">
                    <asp:Label ID="lblCustomerID" runat="Server" Text='<%#Eval("_CustomerID") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblContactName" runat="Server" Text='<%#Eval("_ContactName") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblCompany" runat="Server" Text='<%#Eval("_CompanyID") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblCreatedBy" runat="Server" Text='<%#Eval("_CreatedBy") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblCreatedDate" runat="Server" Text='<%#Eval("_CreatedDate") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblModifiedBy" runat="Server" Text='<%#Eval("_ModifiedBy") %>' />
                </td>
                <td align="left">
                    <asp:Label ID="lblModifiedDate" runat="Server" Text='<%#Eval("_ModifiedDate") %>' />
                </td>
                <td align="left">
                    <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
                </td>
                <td align="left">
                    <asp:LinkButton ID="Delete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

以及背后的代码

protected void lvItemView_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            System.Web.UI.WebControls.Label index_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCustomerID");
            int customerID = int.Parse(index_id.Text);
            Item item = new Item();
            item._CustomerID = customerID;

            System.Web.UI.WebControls.Label cmp_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCompany");
            int companyID = int.Parse(cmp_id.Text);
            item._CompanyID = companyID;

            System.Web.UI.WebControls.Label samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblContactName");
            item._ContactName = samp.Text;

            samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedBy");
            item._CreatedBy = samp.Text;

            samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedDate");
            item._CreatedDate = samp.Text;

            samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedBy");
            item._ModifiedBy = samp.Text;

            samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedDate");
            item._ModifiedDate = samp.Text;

            CustomerID = item._CustomerID.ToString();
            ContactName = item._ContactName.ToString();
            CompanyID = item._CompanyID.ToString();
            CreatedBy = item._CreatedBy.ToString();
            CreatedDate = item._CreatedDate.ToString();
            ModifiedBy = item._ModifiedBy.ToString();
            ModifiedDate = item._ModifiedDate.ToString();

            modAdd.Show();
        }

我是 asp.net 和 c# 的新手,所以我不知道如何处理这种错误。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    OnItemEditing中设置lvItemView_ItemEditing事件,点击编辑按钮时触发

    <asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder"  OnItemEditing="lvItemView_ItemEditing">
    

    如果你绑定pageload,还可以设置数据源

    lvItemView.DataSource = SomeData;
    lvItemView.DataBind();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多