【问题标题】:Binding generic list of objects to ListView and then updating the bound list将通用对象列表绑定到 ListView,然后更新绑定列表
【发布时间】: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


    【解决方案1】:

    我会说,不,这不是“最简单”的方式。

    您是否考虑过使用数据源控件?在这种情况下,诸如 ObjectDatasource 或 SQLDataSource 控件之类的东西非常有用。

    您还可以使用其他模板,例如,更新应使用EditItemTemplate。我进行了快速搜索,为您提供了一些有关如何最好地利用 Listview 控件的更详细信息的链接。例如,您会注意到您不必拨打FindControl 电话。 Link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2015-01-16
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多