views:

37

answers:

3

I have the following code which I thought would give me the ability to edit a record in my list view but when i click edit i get a postback but am not able to 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>
A: 

Are you databinding your ListView to anything? If it's not databound, then you're going to have to manually specify the record that you want to edit by handling the ItemEditing event.

protected void MyListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
    ListView1.EditIndex = e.NewEditIndex;
        // Re-databind here
}
womp
I databind on pageload. Do i need to do it anywhere else? It displays records fine, i just can't edit them.
Abe Miessler
A: 

Based on comments, don't databind on every postback unless you have ViewState turned off.

private void Page_Load()
{
    if (!IsPostBack)
    {
       //databind
    }
}
rick schott
Yeah that's actually what i'm doing.
Abe Miessler
A: 

It looks like i just needed to add an OnItemEditing event to my ListView declaration and the function to back it up. I've updated my code snippit above to reflect the changes made in the aspx file.

Abe Miessler