【发布时间】:2011-12-06 20:48:31
【问题描述】:
我有一个绑定到我创建的通用对象列表的 ListView。
<asp:ListView ID="lvwConfig" runat="server">
<ItemTemplate>
<br />
<div class="title_small">
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'/>
</div>
<asp:TextBox ID="iFirstValue" runat="server" MaxLength="8" Text='<%#Eval("FirstValue")%>'></asp:TextBox><br />
<asp:TextBox ID="iSecondValue" runat="server" MaxLength="8" Text='<%#Eval("SecondValue")%>'></asp:TextBox><br />
<asp:TextBox ID="iThirdValue" runat="server" MaxLength="8" Text='<%#Eval("ThirdValue")%>'></asp:TextBox><br />
</ItemTemplate>
</asp:ListView>
protected void btnSave_Click(object sender, EventArgs e)
{
//Loop through each item in the listview
for (int i = 0; i < lvwSMSConfig.Items.Count(); i++)
{
//Some code to check to see if the value was updated
//If it was, call UpdateItem
lvwSMSConfig.UpdateItem(i,true);
}
}
protected void lvwSMSConfig_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
{
TextBox iFirstValue= (TextBox)lvwSMSConfig.Items[e.ItemIndex].FindControl("iFirstValue");
TextBox iSecondValue= (TextBox)lvwSMSConfig.Items[e.ItemIndex].FindControl("iSecondValue");
TextBox iThirdValue= (TextBox)lvwSMSConfig.Items[e.ItemIndex].FindControl("iThirdValue");
myObjectList[e.ItemIndex].FirstValue= iFirstValue.Text;
myObjectList[e.ItemIndex].SecondValue= iSecondValue.Text;
myObjectList[e.ItemIndex].ThirdValue= iThirdValue.Text;
}
上面的代码(修改了一些用于公开发布的代码)工作得很好,但是我不确定这是否是实现我的目标的最佳方式。我应该采取更直接的路线吗?
【问题讨论】:
标签: c# asp.net listview data-binding