简体   繁体   English

C#=未处理的ListView引发事件。

[英]C# = The ListView raised event which wasn't handled.

There is a ListView which shows the data that were retrieved from the database. 有一个ListView显示从数据库检索的数据。

I want to edit a single record on a ListView by clicking the edit button beside it but if I press the edit button, I get the error: 我想通过单击列表视图旁边的编辑按钮来编辑ListView上的单个记录,但是如果按编辑按钮,则会出现错误:

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

Here is the ListView: 这是ListView:

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

And the code behind 和后面的代码

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

I am new to asp.net and c# so I have no idea what to do with this kind of error. 我是asp.net和C#的新手,所以我不知道该如何处理这种错误。

set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button 设置事件lvItemView_ItemEditingOnItemEditing将被触发时,点击编辑按钮

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

and also set data source if you are binding on pageload 并设置数据源(如果您绑定到pageload

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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