2011-07-26 55 views
0

我有一個DetailsView控件中的列表框,分別具有不同的(SQL)數據源:如何從DetailsView中的ListBox中的SqlDataSource訪問訪問字段?

<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」現在看來,試圖從與相關的SqlDataSource閱讀將引發DetailsView作爲異常:「DataBinding:System.Data.DataRowView不包含名爲NumberOfRows的屬性」。

從ListBox的SqlDataSource返回的內容填充ListBox的項目不是問題,但是如何從其他列訪問數據(如果可能的話)?只需在行中輸​​入列名稱=「NumberOfRows」當然不起作用。

更新

什麼我真的喜歡做澄清:我想ListBox的「行」屬性被動態地設置爲項目的數量,也就是說,如果有六個項目,「行「應設置爲6,因此不需要滾動,並且列表框中沒有空白空間。

在此先感謝。

G.

回答

0

不知道這完全是你想要的,但你可以試試這個:

// 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; 

希望這將至少讓你在正確的方向前進。

+0

不幸的是,這並沒有幫助 - 我可以訪問和設置行屬性,但這不反映在用戶界面中。 – Gorgsenegger

+0

@Gorgsenegger - 你能詳細說明/澄清你想做什麼嗎? – Tim

+0

更新的問題 – Gorgsenegger