我有一個硬編碼值的下拉列表:綁定下拉用硬編碼值
<asp:DropDownList ID="BulletinTypeDropDown" runat="server">
<asp:ListItem Selected="True" Text="--Select--" Value="--Select--"></asp:ListItem>
<asp:ListItem Text="News" Value="News"></asp:ListItem>
<asp:ListItem Text="report" Value="report"></asp:ListItem>
<asp:ListItem Text="System" Value="System"></asp:ListItem>
<asp:ListItem Text="Reminder" Value="Reminder"></asp:ListItem>
</asp:DropDownList>
我想用它來從數據庫插入和編輯值。例如,在添加記錄時,用戶選擇「報告」並將「報告」插入到數據庫中。然後,如果用戶由於db中的值爲'report'而去編輯頁面,那麼下拉列表必須顯示'report'被選中。 但我能夠成功將值存儲在數據庫中,但在編輯頁面中我無法檢索所選頁面。 我正在使用實體框架工作,所以,我厭倦了很多方式,但無法獲得編輯頁面中的值。 這是我試過的方式。
DropDown.SelectedValue = bulList.intype.ToString();
// intype是我從數據庫中獲取的值,它返回的是正確的值,但在DropDown.selectedvalue中傳遞null。
有人可以請幫我什麼是我可以工作,以獲得我的下拉列表中的值的其他方式。
感謝
您在頁面生命週期的哪個事件處理程序中嘗試設置下拉列表值(Page_load,Button_click)?編輯頁面如何獲得該值?當你顯示更多的代碼時,它更容易。 – 2012-02-06 21:15:49
我試圖展現在page_load.I的下拉列表剛剛硬編碼的值,並在下拉列表並在後面的代碼我累了我只是做如下,如果(bulletinList.Bulletintype!= NULL){ BulletinTypeDropDown.SelectedValue = bulletinList.Bulletintype;} – user1098449 2012-02-06 21:18:15
你什麼時候從數據庫設置讀取值?也許你的下拉菜單在設置SelectedValue時沒有硬編碼的Items?在設置了SelectedValue的行上中斷並檢查Items.Length。 – 2012-02-06 21:19:45