如何同时编辑ASP.NET ListView控件中的所有行?

时间:2022-10-30 10:46:46

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.

我想知道如何将所有ListView行一次性置于编辑模式。我不是在寻找一次编辑每一行的传统行为。答案可以是C#或VB.NET。

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

此外,如果可能,在编辑所有行之后保存每行更改的任何示例代码。

1 个解决方案

#1


10  

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

可能最简单的方法是使用ListView的ItemTemplate,因此实质上,ListView始终处于“编辑模式”:

<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...
        }
    }
}

#1


10  

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

可能最简单的方法是使用ListView的ItemTemplate,因此实质上,ListView始终处于“编辑模式”:

<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...
        }
    }
}