How do I edit all rows in an ASP.NET ListView control at the same time?

I would like to know how to put all of my ListView rows into edit mode at once. I am not looking for the traditional behavior of editing each row one at a time. The answer can be in either C# or VB.NET.

Also, if possible, any sample code of saving each row's changes after all rows have been edited.

Answers


Probably the easiest way is to just use the ListView's ItemTemplate, so in essence, the ListView is always in "edit mode":

<asp:ListView 
    ID="lvwDepartments" 
    runat="server" 
    DataKeyNames="department_id" 
    DataSourceID="sqlDepartments" 
    ItemPlaceholderID="plcItem">

    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("department_id") %>
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtDepartmentName" Text='<%# Eval("dname") %>' Columns="30" />
            </td>
        </tr>
    </ItemTemplate>
    <EmptyDataTemplate>
        <p>
            No departments found.
        </p>
    </EmptyDataTemplate>
    <LayoutTemplate>
        <table>
            <thead>
                <tr>
                    <th>Department ID</th>
                    <th>Name</th>
                </tr>
            </thead>
            <tbody>
                <asp:PlaceHolder runat="server" ID="plcItem" />
            </tbody>
        </table>
    </LayoutTemplate>
</asp:ListView>

<asp:SqlDataSource 
    ID="sqlDepartments" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:HelpDeskConnectionString %>" 
    SelectCommand="SELECT * FROM [departments]" />

<asp:Button runat="server" ID="cmdSave" Text="Save Changes" OnClick="cmdSave_Click" />

You can then read the changed values when the user clicks the button:

protected void cmdSave_Click ( object sender, EventArgs e )
{
    foreach ( ListViewItem item in lvwDepartments.Items )
    {
        if ( item.ItemType == ListViewItemType.DataItem )
        {
            TextBox txtDepartmentName = ( TextBox ) item.FindControl( "txtDepartmentName" );

            // Process changed data here...
        }
    }
}

Need Your Help

Compartment items not displayed in DSL diagram

visual-studio diagram dsl

OK, so things have progressed significantly with my DSL since I asked this question a few days ago.

Order by statement with start value in a Round Robin Select

sql tsql sql-order-by round-robin

I have a table in SQL Server where every row is a mail to deliver. Every email belongs to a domain.

Gradient textfields in panel with TitledBorder - performance issue

java swing gradient jtextfield paintcomponent

I have a panel with 3 inner panels that all use a TitledBorder. The third inner panel has a number of custom textfields that are painted with GradientPaint. The textfields come from a custom class ...