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

标签 asp.net listview

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

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

最佳答案

可能最简单的方法是仅使用 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" />

然后,当用户单击按钮时,您可以读取更改的值:

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

关于asp.net - 如何同时编辑 ASP.NET ListView 控件中的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/315349/

相关文章:

java - 应用程序的布局和 View 可见性

java - 确定 ListView 位置何时发生变化

c# - 使用字符串值创建新实例

当模态弹出窗口重新打开时,jQuery document.ready 会触发吗?

c# - 如何避免每次回发都重新加载GridView数据?

asp.net - Orchard CMS - 配置基本 URL

c# - 更改 System.Dynamic.ExpandoObject 默认行为

c# - 具有自定义数据的ListView

android - 动态添加多个 View 到 ListView 行

android - 在哪里放置 notifyDataSetChanged();在 customAdapter 中,因此 listView 不会在滚动时丢失数据