简体   繁体   中英

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

There is a ListView which shows the data that were retrieved from the database.

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:

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

Here is the 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.

set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button

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

and also set data source if you are binding on pageload

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

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