2
votes

I don't know why i got this error but my code i think is correct.

HTML

<asp:ListView ID="lvwData" runat="server" OnItemCommand="Student_OnItemCommand" EnableViewState="true"
    DataKeyNames="id">
    <LayoutTemplate>
        <table class="table-list table-list-gridded" id="dataTable" border="0" cellspacing="0"
            cellpadding="5">
            <thead>
                <th class="id">
                    ROW #
                </th>
                <th>
                    Student ID
                </th>
                <th>
                    First Name
                </th>
                <th>
                    Last Name
                </th>
                <th>
                    Course
                </th>
                <th class="action">
                    <span>ACTIONS</span>
                </th>
            </thead>
            <tbody id="itemPlaceholder" runat="server">
            </tbody>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td align="center" class="id">
                <%# Container.DataItemIndex + 1 %>
            </td>
            <td>
                <asp:Label ID="lblStudentID" runat="server" Text='<%# Eval("student_id") %>' />
            </td>
            <td>
                <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("firstname") %>' />
            </td>
            <td>
                <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("lastname") %>' />
            </td>
            <td>
                <asp:Label ID="lblCourse" runat="server" Text='<%# Eval("course") %>' />
            </td>
            <td align="center" class="action">
                <asp:LinkButton ID="lnkEdit" class="btn btn-small" Text="Edit" CommandName="Edit"
                    CommandArgument='<%# Eval("ID") %>' runat="server"><i class="icon-pencil"></i></asp:LinkButton>
                <asp:LinkButton ID="DeleteLinkButton" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
                    OnClientClick="return confirm('Are you sure you want to delete this Employee?');"
                    runat="server" class="btn btn-small"><i class="icon-remove"></i></asp:LinkButton>
            </td>
    </ItemTemplate>
    <EmptyDataTemplate>
        <div class="centered alert alert-error">
            No list of records to display.
        </div>
    </EmptyDataTemplate>
</asp:ListView>

C# codes behind:

protected void Student_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Delete":
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            string employeeID = lvwData.DataKeys[dataItem.DisplayIndex].Value.ToString();

            DBConnect db = new DBConnect();
            db.Delete("delete from students where id = '" + employeeID + "'");
            Response.Redirect("~/Users/Student.aspx");
            break;
        case "Edit":
            mvStudents.ActiveViewIndex = 1;
            break;
    }
    //if (String.Equals(e.CommandName, "Delete"))
    //{

    //}
    //else if (string.Equals(e.CommandName, "Edit"))
    //{
    //    ListViewDataItem dataItem = (ListViewDataItem)e.Item;
    //    string employeeID = lvwData.DataKeys[dataItem.DisplayIndex].Value.ToString();


    //}
}

Stack Trace:

[InvalidOperationException: The ListView 'lvwData' raised event ItemEditing which wasn't handled.] System.Web.UI.WebControls.ListView.OnItemEditing(ListViewEditEventArgs e) +305573 System.Web.UI.WebControls.ListView.HandleEdit(Int32 itemIndex) +51 System.Web.UI.WebControls.ListView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +324 System.Web.UI.WebControls.ListView.OnBubbleEvent(Object source, EventArgs e) +227 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.ListViewDataItem.OnBubbleEvent(Object source, EventArgs e) +112 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9528578 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

1
sorry i change it lvwData to listview1 i thought it will be more easy to understand if i will put listview1Denmark
Please include the error stack trace here too.Brian Mains
stack trace included now..Denmark

1 Answers

1
votes

i would suggest you that change your command name Reason:- the CommandName of a clicked button is “Edit” then it will put the ListView in the Edit mode, try modifying the CommandName=”Edit” to something like “modify”… its so easy

hope you understand.