熟悉DropDownList和ObjectDataSource的配合使用
Categories DropDownList完成后, 下一步就需要一個顯示屬于選定類別的產品的DropDownList了. 要完成這個功能, 再增加一個DropDownList控件并命名為ProductsByCategory. 與Categories DropDownList一樣, 為ProductsByCategory DropDownList創建一個新的ObjectDataSource并命名為ProductsByCategoryDataSource.
DropDownList和ObjectDataSource: 為ProductsByCategory DropDownList添加新數據源
DropDownList和ObjectDataSource: 創建一個新的 ObjectDataSource 并命名為ProductsByCategoryDataSource
由于ProductsByCategory DropDownList 需要顯示屬于選定類別的產品, 讓ObjectDataSource 調用ProductsBLL對象的GetProductsByCategoryID(categoryID)方法.
DropDownList和ObjectDataSource: 選擇使用ProductsBLL類
DropDownList和ObjectDataSource: 配置ObjectDataSource 使用GetProductsByCategoryID(categoryID)方法
在向導的***一步需要指定categoryID的值. 我們把Categories DropDownList的已選擇的數據項項作為該參數值.
DropDownList和ObjectDataSource: 使用categoryID參數從Categories DropDownList接收值
ObjectDataSource配置完成后, 剩下的就是指定DropDownList要顯示的字段和作為值的字段了. 設置為顯示ProductName并把ProductID做為值字段
DropDownList和ObjectDataSource: 指定 DropDownList數據項的文本和值使用的數據源字段
ObjectDataSource 和ProductsByCategory DropDownList配置完成后頁面上會有兩個DropDownList: ***個列出所有的類別,第二個列出屬于選定類別的產品. 當用戶在***個DropDownList上選擇了一個新的類別后, 將會發生一次回發(postback),第二個DropDownList將會重新綁定以顯示屬于新選定類別的產品. 圖12 和圖13顯示了在瀏覽器中看到的MasterDetailsDetails.aspx頁面.
DropDownList和ObjectDataSource: ***次訪問頁面時Beverages 類別是選中的.
DropDownList和ObjectDataSource: 選擇一個不同的類別時顯示該類別的產品
DropDownList和ObjectDataSource: 激活productsByCategory DropDownList的 AutoPostBack屬性
【編輯推薦】