2011-06-30 60 views
2

我已經將一個DropDownList控件添加到DetailsView中,並且我試圖讓它爲我的更新/插入方法綁定到「狀態」參數。DetailsView中的下拉列表

<InsertItemTemplate> 
<asp:DropDownList ID="States" runat="server" SelectedValue='<%# Bind("State") %>'> 
<asp:ListItem Value="NY">New York</asp:ListItem> 
.... 
</asp:DropDownList> 
</InsertItemTemplate> 

我已填充DropDownList中有50個州,所以他們沒有綁定到任何表是靜態的項目。我只想在編輯時選擇一個狀態,當我點擊編輯時,它應該運行存儲過程來編輯並將DropDownList的值作爲@State的參數。

不幸的是,我得到這個錯誤:

'States' has a SelectedValue which is invalid because it does not exist in the 
list of items. 
Parameter name: value 

我研究這個問題,但一些帖子使用,而不是「綁定」或添加一些空值「評估和演示」的建議,但並沒有解決我的問題。你們對我如何才能擺脫這個錯誤有任何想法/建議嗎?

+0

'時,我打edit'你不應該在'EditTemplate'看呢? –

+0

是的,我的意思是當你填入EditTemplate後,你點擊更新按鈕,它應該更新。 –

回答

2

如果ListItems是靜態的(即在標記中定義),我相信你也應該發佈它們。 記住listItems中的正確的語法是

<asp:ListItem Value="value" Text="displayText"></asp:ListItem> 
+0

我使用單個列表項更新了OP。所有其他項目具有相同的結構;只是不同的文字。我相信格式也是正確的,對嗎? –

+0

檢查數據源中是否有寄存器中的「狀態」值未在ListItems中列出。 – KevinCampusano

0

這個錯誤輪番上漲,如果在數據庫中的表中的一些值不符合您的列表項value..so檢查表項

0

將其綁定到項目模板值例如

然後,你可以有:

InsertItemTemplate> 
<asp:DropDownList ID="States" runat="server" SelectedValue='<%# Bind("State") %>'> 
<asp:ListItem Value="NY">New York</asp:ListItem> 
.... 
</asp:DropDownList> 
</InsertItemTemplate>