Cannot get editing to work with ListView

I have the following code that I thought would give me the opportunity to edit an entry in my list, but when I click edit, I get a postback, but I can’t edit anything. Am I doing something wrong?

<asp:ListView ID="lv_Personnel" runat="server" OnItemEditing="lv_Personnel_ItemEditing">
            <LayoutTemplate>
                <table cellpadding="2" border="1" runat="server" id="tbl_Personnel">
                    <tr id="headerRow" runat="server">
                        <th>
                        </th>
                        <th>
                            Level of Staff
                        </th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder" />
                    <tr runat="server" id="insertPlaceholder" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr runat="server">
                    <td>
                        <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                    </td>
                    <td>
                        <%# Eval("LineDescription")%>
                    </td>
                </tr>
            </ItemTemplate>
            <EditItemTemplate>
                <tr runat="server" style="background-color: #ADD8E6">
                    <td>

                    </td>
                    <td>
                        Level of Staff:
                        <asp:TextBox ID="tb_LevelOfStaff" runat="server" Text='<%# Eval("LineDescription") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
        </asp:ListView>
+3
source share
3 answers

Looks like I just need to add the OnItemEditing event to my ListView declaration and a function to back it up. I updated my snapshot above to reflect the changes made to the aspx file.

-1
source

ListView ? , , , ItemEditing.

protected void MyListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
    ListView1.EditIndex = e.NewEditIndex;
        // Re-databind here
}
+1

Based on the comments, do not bind data to each postback unless you have disabled ViewState.

private void Page_Load()
{
    if (!IsPostBack)
    {
       //databind
    }
}
0
source

Source: https://habr.com/ru/post/1736223/


All Articles