How can I access access fields from a SqlDataSource in a ListBox within a DetailsView?
I've got a ListBox within a DetailsView, each having a different (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>
....
Trying to access the column "NumberOfRows" now seems to try and read it from the SqlDataSource that is associated with the DetailsView as an exception is thrown: "DataBinding: System.Data.DataRowView does not contain a property with the name NumberOfRows".
Filling the items of the ListBox with what is returned from the SqlDataSource for that ListBox is not a problem, but how can I access data from other columns (if at all possible)? Just entering the column name as in Rows="NumberOfRows" does of course not work.
Update
Clarification of what I would actually like to do: I want the "Rows" property of the ListBox to be set dynamically to the number of items, i.e. if there are six items, "Rows" should be set to six so no scrolling is neccessary and not empty space is in the listbox.
Thanks in advance.
G.
Not sure if this is entirely what you want, but you can try this:
// 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;
Hopefully that will at least get you going in the right direction.
精彩评论