3

すべての ListView 行を一度に編集モードにする方法を知りたいです。各行を 1 つずつ編集する従来の動作を探しているわけではありません。答えは、C# または VB.NET のいずれかにある可能性があります。

また、可能であれば、すべての行が編集された後に各行の変更を保存するサンプル コード。

4

1 に答える 1

10

おそらく最も簡単な方法は、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...
        }
    }
}
于 2008-11-25T16:30:42.877 に答える