I have an ObjectDataSource for my FormView (Students) and another ObjectDatSource for the DropDownList (Name) within the FormView. If the DropDownList’s source does not contain a name value matching the formview’s source, I would like to display “Not available”. Currently I have this code, which works if the datasource returns a NULL value. How can I change this to display “Not Available” when the FormView name value is not in the DropDownList’s databound list?
<asp:FormView ID="Students" runat="server" DataSourceID="Students_DataSource">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<EditTemplate>
<asp:DropDownList ID="ddlName" runat="server"
SelectedValue='<%# If(Eval("Name") IsNot Nothing, Eval("Name"), "Not Available") %>'
DataSourceID="fvAllNames" runat="server" DataTextField="Name" DataValueField="Name" />
</EditTemplate>
</asp:FormView>
In your databound controls, you can get at the new values in the itemtemplate with your typical
What I’m actually doing here is binding the controls to a list of actual objects instead of the datatable that is constructed with the datasource control. By doing that, I can do whatever I need to do with those lists before binding it to the control. In this case, I bump the two lists together and add items for those that do not exist in one list but exist in the other. Not sure if this is exactly what you needed, but this should be enough to give you some ideas.