开发者

ASP.NET Drop Down List error with null values in SqlDataSource

开发者 https://www.devze.com 2022-12-17 06:01 出处:网络
I have a drop down list on a form view which are both bound to different data sources. When trying to run the program I am getting \'ddlFieldName\' has a SelectedValue which is invalid because it doe

I have a drop down list on a form view which are both bound to different data sources.

When trying to run the program I am getting 'ddlFieldName' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value.

I think it is because th开发者_如何学运维e field associated with the drop down list contains a null value which isnt in the referencing table. I have tried to work round this by adding an empty item in the list but it doesnt seem to be working:

<asp:DropDownList ID="ddlAgency" runat="server" 
DataSourceID="dsAgency" DataTextField="Agency" 
DataValueField="AgencyID" SelectedValue='<%# Bind("Agency") %>'  >
<asp:ListItem></asp:ListItem></asp:DropDownList>

Can anyone suggest a way round this?

Thanks

anD666


Try adding AppendDataBoundItems="true" to the DropDownList as one if its properties. See http://weblogs.asp.net/scottgu/archive/2006/01/29/436804.aspx


One workaround could be.

Try to assign Selected value in code behind. There you can check for null value easily.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号