cannot get selected value of drop down list in form view edit

I have a form view with an embedded dropdown list for edits:

           <asp:FormView ID="fvSA" runat="server" AllowPaging="True" DataKeyNames="ID"
                DataSourceID="ObjectDataSourceSA" Width="50%"
                OnItemInserting="fvSA_OnInserting"
                OnItemInserted="fvSA_OnInserted"
                OnItemUpdating="fvSA_OnUpdating"
                OnItemUpdated="fvSA_OnUpdated"
                OnDataBound = "fvSA_DataBound">
                <EditItemTemplate>
                    <b>Sales Area ID:</b>
                    <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                    <br />
                    <b>Sales Area:</b>
                    <asp:TextBox ID="SalesAreaTextBox" runat="server"
                        Text='<%# Bind("SalesArea") %>' />
                    <br />
                    <b>Contact Email:</b>
                    <asp:TextBox ID="ContactTextBox" runat="server" width = "255px" Text='<%# Bind("Contact") %>' />
                    <br />
                    <b>Sales Theatre:</b>
                     <asp:DropDownList ID="ddlEditSalesTheatre" runat="server"
                            AppendDataBoundItems="true"
                            DataTextField="SalesTheatre"
                            DataValueField="ID"
                            DataSourceID="SqlDataSourceSalesTheatre">
                        </asp:DropDownList>
                     <br />
                     <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                        CommandName="Update" Text="Update" />
                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
                        CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <b>Sales Area:</b>
                    <asp:TextBox ID="SalesAreaTextBox" runat="server"
                        Text='<%# Bind("SalesArea") %>' />
                    <br />
                    <b>Contact Email:</b>
                    <asp:TextBox ID="ContactTextBox" runat="server" width = "255px" Text='<%# Bind("Contact") %>' />
                    <br />
                    <b>Sales Theatre:</b>
                    <asp:DropDownList ID="ddlInsertSalesTheatre" runat="server"
                            AppendDataBoundItems="true"
                            DataTextField="SalesTheatre"
                            DataValueField="ID"
                            DataSourceID="SqlDataSourceSalesTheatre">
                    </asp:DropDownList>
                    <br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
                        CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
                        CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
                <ItemTemplate>
                    <b>Sales Area ID:</b>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                    <br />
                    <b>Sales Area:</b>
                    <asp:Label ID="SalesAreaLabel" runat="server"
                        Text='<%# Bind("SalesArea") %>' />
                    <br />
                    <b>Contact Email:</b>
                    <asp:Label ID="ContactLabel" runat="server" width = "255px" Text='<%# Bind("Contact") %>' />
                    <br />
                    <b>Sales Theatre:</b>
                    <asp:Label ID="SalesTheatreIDLabel" runat="server"
                        />
                    <br />
                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
                        CommandName="Edit" Text="Edit" />
                    &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
                        CommandName="Delete" Text="Delete" />
                    &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
                        CommandName="New" Text="New" />
                </ItemTemplate>
            </asp:FormView>

The drop down lists render well and I can select one on edit/insert, but during the oninserting / onupdating event the selected value always comes out to be the --select-- value i add ondatabound (not included here for brevity). Can someone tell me where I'm going wrong here?

 Protected Sub fvSA_OnUpdating(ByVal sender As Object, ByVal Args As FormViewUpdateEventArgs)
        Dim ddlEditSalesTheatre As DropDownList = CType(fvSA.Row.FindControl("ddlEditSalesTheatre"), DropDownList)
        If ddlEditSalesTheatre.SelectedValue = "--select--" Then
            Args.Cancel = True
            Throw New Exception("Select a valid Sales Theatre!") -- I always get this exception!
        End If
        If Args.Cancel = True Then
            lblDetails.Text = "<i>Record " & Args.NewValues("SalesArea") & " not added.</i>"
        Else
            lblDetails.Text = "<i>Record " & Args.NewValues("SalesArea") & " added.</i>"
       End If
    End Sub
acdagirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

raja_ind82Commented:
0
acdagirlAuthor Commented:
this doesn't explain why I can't see the correct value in mine. the person who asked the question eventually solved it by started a new project and not explaining their approach. I have used the above approach before and I believe it worked. I'm just not seeing the error here and would like some assistance on debugging this particular issue.
0
safitechCommented:
I had a similar issue where if I set a value in the DataBound event then I lost the data binding.  That may be what you're finding here.

If you just want to include a list item with "--select--" you can do that as follows.  If you remove the code from OnDataBound then you should be able to retain your bindings.  (The Value attribute could be 0 or something rather than "--select--" if you prefer.)

                    <asp:DropDownList ID="ddlInsertSalesTheatre" runat="server"
                            AppendDataBoundItems="true"
                            DataTextField="SalesTheatre"
                            DataValueField="ID"
                            DataSourceID="SqlDataSourceSalesTheatre">
                        <asp:ListItem Text="-- select --" Value="-- select --" />
                    </asp:DropDownList>

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

acdagirlAuthor Commented:
i'll try this...
0
acdagirlAuthor Commented:
I tried and I still get the selected value to the top level item. for some reason it's just not registering the actual selected value...
0
safitechCommented:
Oops, I think I just copied and pasted your code and added the AppendDataBoundItems bit.  Of course you'll still need to include a binding.  So assuming you want the value to go into the SalesArea column/property, you'd write:

                    <asp:DropDownList ID="ddlInsertSalesTheatre" runat="server"
                            AppendDataBoundItems="true"
                            DataTextField="SalesTheatre"
                            DataValueField="ID"
                            DataSourceID="SqlDataSourceSalesTheatre"
                            SelectedValue='<%# Bind("SalesArea") %>'
                     >
                        <asp:ListItem Text="-- select --" Value="-- select --" />
                    </asp:DropDownList>

By the way, if you want to add a required field validator you can use something like:

            <asp:RequiredFieldValidator
                id="reqValSalesTheatre"
                ControlToValidate="ddlInsertSalesTheatre"
                InitialValue="-- select --"
                Text="!"
                ErrorMessage="Please select a Sales Theatre"
                runat=server
                />

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
acdagirlAuthor Commented:
ok, I'll try this...
0
acdagirlAuthor Commented:
thanks for your help, but i think there's something else wrong with my code... i retried it using an object data source and still the same issue... but thanks for the tip about the validator.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.