• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1452
  • Last Modified:

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
0
acdagirl
Asked:
acdagirl
  • 5
  • 2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now