【问题标题】:How can I access access fields from a SqlDataSource in a ListBox within a DetailsView?如何从 DetailsView 中 ListBox 中的 SqlDataSource 访问访问字段?
【发布时间】:2011-07-27 16:30:16
【问题描述】:

我在 DetailsView 中有一个 ListBox,每个都有不同的 (Sql)DataSource:

<asp:DetailsView ID="dvwSomeDetailsView" DataSourceID="sdsSomeDetailsView" runat="server" ...
    ...
    <asp:TemplateField HeaderText="SomeHeaderText">
        <ItemTemplate>
            <asp:ListBox ID="lstSomeListBox" runat="server" DataSourceID="sdsSomeListBox" DataTextField="SomeColumn" DataValueField="SomeOtherColumn" Rows='<%#Eval("NumberOfRows") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    ....
</asp:DetailsView>
....

尝试访问列“NumberOfRows”现在似乎尝试从与 DetailsView 关联的 SqlDataSource 中读取它,因为抛出异常:“DataBinding:System.Data.DataRowView 不包含名称为 NumberOfRows 的属性”。

用 SqlDataSource 为该 ListBox 返回的内容填充 ListBox 的项目不是问题,但我如何访问其他列的数据(如果可能的话)?仅仅像 Rows="NumberOfRows" 那样输入列名当然是行不通的。

更新

澄清我真正想做的事情:我希望将 ListBox 的“Rows”属性动态设置为项目数,即如果有六个项目,“Rows”应该设置为六个所以没有滚动是必要的,并且列表框中没有空白空间。

提前致谢。

G.

【问题讨论】:

    标签: asp.net listbox sqldatasource detailsview


    【解决方案1】:

    不确定这是否完全是你想要的,但你可以试试这个:

    // Get the ListBox - I used the first row for sake of illustration
    ListBox lb = (ListBox)dvwSomeDetailsView.Rows[0].FindControl("lstSomeListBox");
    
    // Now you can access the ListItemCollection of the ListBox
    string value = lb.Items[0].Value;
    

    希望这至少能让你朝着正确的方向前进。

    【讨论】:

    • 不幸的是,这没有帮助 - 我可以访问和设置 Rows 属性,但这不会反映在 UI 中。
    • @Gorgsenegger - 你能详细说明/澄清你想要做什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多