dropdownlist in FormView "has a SelectedValue which is invalid"

G

Guest

In my Registrant FormView I have a DropDownList which loads data from a
secondary SqlDataSource "sdsOfficeParks". I need the user to select an office
park but save the selected value in the FormView's ObjectDataSource
"odsRegistrant" .

The following solution can't work because I need the ddl selected value
bound to my odsRegistrant but both tables have the same column name for
primary key.

<asp:DropDownList ID="ddlOfficePark" runat="server" CssClass="waDdl"
Width="202px"
DataSourceID="sdsOfficePark" DataTextField="OfficeParkName"
DataValueField="OfficeParkId"
SelectedValue='<%# Bind("OfficeParkId")%>' >

<asp:SqlDataSource ID="sdsOfficePark" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:myDB %>"
SelectCommand="Select OfficeParkName, OfficeParkId from dbo_OfficePark
order by OfficeParkName ">
</asp:SqlDataSource>

<asp:ObjectDataSource ID="odsRegistrant" runat="server"
TypeName="RegistrantDB"
DeleteMethod="DeleteRegistrant" InsertMethod="InsertRegistrant"
SelectMethod="GetRegistrant"
UpdateMethod="UpdateRegistrant" DataObjectTypeName="RegistrantDetails" >
<UpdateParameters>
<asp:parameter Name="officeParkId" Type="Int32"/>
....

Note that for most rows in the database the OfficeParkId is 0 and there is
no zero entry in the ddl. Could this be the problem and how do I load a
(choose one) entry before the data is loaded?

Thanks for any ideas.
 
G

Guest

Good Morning Dabbler,

Blessed Good Friday to you and the readers.

In the demo on my website I explained how I used the AppendDataBoundItems
property to set a ListItem with null value as the default within the
InsertItemTemplate.

<%-- when we insert a new record there is no country value to be bound upon
displaying the empty record. Therefore let's add a default listitem with a
null
value so that we can validate it using a RequiredFieldValidator to ensure that
the user will enter a value in it. I will use here the AppendDataBoundItems
property of the dropdownlist so that any databound items to be created will
not
replace the default listitem that I just created. --%>
<asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountries"
AutoPostBack="True"
SelectedValue='<%# Bind("Country") %>' AppendDataBoundItems="true">
<asp:ListItem Value="">Select a Country</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ForeColor="white" ID="valddlCountry"
runat="server"
ErrorMessage="Cannot leave the country blank"
Text="*" Display="Dynamic"
ControlToValidate="ddlCountry">
</asp:RequiredFieldValidator>

Let me know if this strategy solved the problem you had or not. May be I
did not fully understand it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top