我如何在同一时间编辑在ASP.NET ListView控件的所有行? [英] How do I edit all rows in an ASP.NET ListView control at the same time?
问题描述
我想知道如何把我所有的ListView行进入编辑模式的一次。我不是在寻找一次修改每行一个传统的行为。答案可以在C#或VB.NET。
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.
另外,如果可能的话,任何样品code的所有行已编辑后保存每行的变化。
Also, if possible, any sample code of saving each row's changes after all rows have been edited.
推荐答案
也许最简单的方式就是使用ListView控件的ItemTemplate,所以在本质上,ListView控件始终处于编辑模式:
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...
}
}
}
这篇关于我如何在同一时间编辑在ASP.NET ListView控件的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!