locked
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. RRS feed

  • Question

  • User2028823583 posted

    I have two dropdown boxes (Department and SubDepartment) that are in a DetailsView.  When I'm in edit mode and make a selection in the Department Dropdown, the selected Department is supposed to then be used as a parameter for the SubDepartment.   I'm putting my code in below for the department and subdepartment, and this is the error I receive when I make a selection in the Department dropdown.  Any idea what I'm doing wrong?  

    Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

     

    <asp:TemplateField HeaderText="Department" SortExpression="Department">
    <EditItemTemplate> <asp:DropDownList ID="ddDepartmentID" runat="server" DataSourceID="dsGetRequisitionDepartments" DataTextField="Department" DataValueField="RequisitionDepartmentID" AutoPostBack="True" AppendDataBoundItems="True" SelectedValue='<%# Bind("RequisitionDepartmentID") %>'><asp:ListItem Text="--Select Department--" Value=""></asp:ListItem></asp:DropDownList> <asp:SqlDataSource ID="dsGetRequisitionDepartments" runat="server" ConnectionString="<%$ ConnectionStrings:abcdConnectionString %>"SelectCommand="proc_RequisitionGetDepartments" SelectCommandType="StoredProcedure"></asp:SqlDataSource> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Department") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>
    <asp:TemplateField HeaderText="Department Sub" SortExpression="DepartmentSub"> <EditItemTemplate> <asp:DropDownList ID="ddDepartmentSubID" runat="server" DataSourceID="dsGetRequisitionDepartmentsSub" DataTextField="DepartmentSub" DataValueField="RequisitionDepartmentSubID" SelectedValue='<%# Bind("RequisitionDepartmentSubID") %>'></asp:DropDownList> <asp:SqlDataSource ID="dsGetRequisitionDepartmentsSub" runat="server" ConnectionString="<%$ ConnectionStrings:abcdConnectionString %>"SelectCommand="proc_RequisitionGetDepartmentsSub" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="ddDepartmentID" Name="RequisitionDepartmentID" PropertyName="SelectedValue" Type="Int32" /></SelectParameters> </asp:SqlDataSource> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("DepartmentSub") %>'></asp:Label> </ItemTemplate>



    Friday, May 31, 2019 1:11 PM

Answers

  • User288213138 posted

    Hi  funluckykitty,
     
    According to your description, I modified the code so that you can "keep" the value selected of DropDownList in the code behind.

    In OnModeChanging event I get the value of the label and store them in Viewstate.

    In OnDataBound event I get the control of DropDownList, then selecting default text by ViewState.

     
    The code:

    Aspx:
    <div>
                <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="200px" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" OnModeChanging="DetailsView1_ModeChanging" OnDataBound="DetailsView1_DataBound" >
                    <Fields>
                        <asp:TemplateField HeaderText="Department" SortExpression="Department">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddDepartmentID" runat="server" AutoPostBack="True" AppendDataBoundItems="true" DataSourceID="SqlDataSource2" DataTextField="Department" DataValueField="RequisitionDepartmentID">
                                    <asp:ListItem Text="--Select Department--" Value=""></asp:ListItem>
                                </asp:DropDownList>
                                                          
                                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [RequisitionDepartmentID], [Department], [Id] FROM [Test4]"></asp:SqlDataSource>
                                                          
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Department Sub" SortExpression="DepartmentSub">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddDepartmentSubID" runat="server" DataSourceID="SqlDataSource3" DataValueField="DepartmentSub" AppendDataBoundItems="true">
                                    <asp:ListItem Text="--Select SubDepartment--" Value=""></asp:ListItem>
                                </asp:DropDownList>                          
                                    
                                <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT * FROM Test6 Where DepartmentSub=@DepartmentSub ">
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="ddDepartmentID" Name="DepartmentSub" PropertyName="SelectedValue" />
                                    </SelectParameters>
                                </asp:SqlDataSource>                                                      
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("DepartmentSub") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                    </Fields>   
                </asp:DetailsView>         
                
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" DeleteCommand="DELETE FROM [Test5] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Test5] ([Id], [Department], [DepartmentSub], [RequisitionDepartmentID]) VALUES (@Id, @Department, @DepartmentSub, @RequisitionDepartmentID)" SelectCommand="SELECT [Id], [Department], [DepartmentSub], [RequisitionDepartmentID] FROM [Test5]" UpdateCommand="UPDATE [Test5] SET [Department] = @Department, [DepartmentSub] = @DepartmentSub, [RequisitionDepartmentID] = @RequisitionDepartmentID WHERE [Id] = @Id">
                    <DeleteParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                        <asp:Parameter Name="Department" Type="String" />
                        <asp:Parameter Name="DepartmentSub" Type="String" />
                        <asp:Parameter Name="RequisitionDepartmentID" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Department" Type="String" />
                        <asp:Parameter Name="DepartmentSub" Type="String" />
                        <asp:Parameter Name="RequisitionDepartmentID" Type="String" />
                        <asp:Parameter Name="Id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
                
            </div>   
    Aspx.cs:
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
    
                Label la = (Label)DetailsView1.FindControl("Label3");
                ViewState["l1"] = la.Text;
    
                Label la2 = (Label)DetailsView1.FindControl("Label4");
                ViewState["l2"] = la2.Text;
    
            }
            protected void DetailsView1_DataBound(object sender, EventArgs e)
            {            
                if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit)
                {
                    DropDownList ddl = (DropDownList)DetailsView1.FindControl("ddDepartmentID");
                    ddl.SelectedItem.Text=ViewState["l1"].ToString(); 
                    //ddl.SelectedValue= ViewState["abc"].ToString(); 
    
                    DropDownList ddl2 = (DropDownList)DetailsView1.FindControl("ddDepartmentSubID");
                    ddl2.SelectedItem.Text = ViewState["l2"].ToString();
                }
            }           
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 5, 2019 3:03 AM

All replies

  • User288213138 posted

    Hi  funluckykitty,
     
    According to your description, I tried to reproduce your problem, and found that Bind("RequisitionDepartmentID") can only Bind the fields of DetailsView. If you are binding a field inside a DropDownList, the value in SelectValue must be more than one.

    If you want to let Department parameter be used as a parameter for the SubDepartment,you can do this by setting the SelectCommand property.

    The code:

    <div>
                <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="200px" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
                    <Fields>
                        <asp:TemplateField HeaderText="Department" SortExpression="Department">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddDepartmentID" runat="server" AutoPostBack="True" AppendDataBoundItems="True" DataSourceID="SqlDataSource2" DataTextField="Department" DataValueField="RequisitionDepartmentID">
                                    <asp:ListItem Text="--Select Department--" Value=""></asp:ListItem>
                                </asp:DropDownList>
                                                          
                                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [RequisitionDepartmentID], [Department], [Id] FROM [Test4]"></asp:SqlDataSource>
                                                          
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Department Sub" SortExpression="DepartmentSub">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddDepartmentSubID" runat="server" DataSourceID="SqlDataSource3" DataValueField="DepartmentSub"></asp:DropDownList>                          
                                    
                                <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT * FROM Test6 Where DepartmentSub=@DepartmentSub ">
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="ddDepartmentID" Name="DepartmentSub" PropertyName="SelectedValue" />
                                    </SelectParameters>
                                </asp:SqlDataSource>                                                      
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("DepartmentSub") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                    </Fields>   
                </asp:DetailsView>         
                
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" DeleteCommand="DELETE FROM [Test5] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Test5] ([Id], [Department], [DepartmentSub], [RequisitionDepartmentID]) VALUES (@Id, @Department, @DepartmentSub, @RequisitionDepartmentID)" SelectCommand="SELECT [Id], [Department], [DepartmentSub], [RequisitionDepartmentID] FROM [Test5]" UpdateCommand="UPDATE [Test5] SET [Department] = @Department, [DepartmentSub] = @DepartmentSub, [RequisitionDepartmentID] = @RequisitionDepartmentID WHERE [Id] = @Id">
                    <DeleteParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                        <asp:Parameter Name="Department" Type="String" />
                        <asp:Parameter Name="DepartmentSub" Type="String" />
                        <asp:Parameter Name="RequisitionDepartmentID" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Department" Type="String" />
                        <asp:Parameter Name="DepartmentSub" Type="String" />
                        <asp:Parameter Name="RequisitionDepartmentID" Type="String" />
                        <asp:Parameter Name="Id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
                
            </div> 

    The result:

    My Table:

    Test4:Test6:

    Best Regards,

    Sam

    Monday, June 3, 2019 9:16 AM
  • User2028823583 posted

    Hi there.  I've tried your sample, and it's close to working, but there is a problem that I'm curious if you know how to overcome. When the details view first loads, the ItemTemplates display the current Department and SubDepartment.  HOWEVER, when in your example, when you click edit, both values are "lost" when the edititemtemplate comes in to play.  So, in a lengthly form, it's easy to miss/forget what the original value was for Department or Sub Department when the Edit button is clicked.  

    How do I "keep" the value selected in the drop down list, IF it's a valid value.  Otherwise I want it to go to the first item in the list, which is  the tect "--Select One--"

    Monday, June 3, 2019 6:26 PM
  • User288213138 posted

    Hi  funluckykitty,
     
    According to your description, I modified the code so that you can "keep" the value selected of DropDownList in the code behind.

    In OnModeChanging event I get the value of the label and store them in Viewstate.

    In OnDataBound event I get the control of DropDownList, then selecting default text by ViewState.

     
    The code:

    Aspx:
    <div>
                <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="200px" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" OnModeChanging="DetailsView1_ModeChanging" OnDataBound="DetailsView1_DataBound" >
                    <Fields>
                        <asp:TemplateField HeaderText="Department" SortExpression="Department">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddDepartmentID" runat="server" AutoPostBack="True" AppendDataBoundItems="true" DataSourceID="SqlDataSource2" DataTextField="Department" DataValueField="RequisitionDepartmentID">
                                    <asp:ListItem Text="--Select Department--" Value=""></asp:ListItem>
                                </asp:DropDownList>
                                                          
                                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT [RequisitionDepartmentID], [Department], [Id] FROM [Test4]"></asp:SqlDataSource>
                                                          
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Department Sub" SortExpression="DepartmentSub">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddDepartmentSubID" runat="server" DataSourceID="SqlDataSource3" DataValueField="DepartmentSub" AppendDataBoundItems="true">
                                    <asp:ListItem Text="--Select SubDepartment--" Value=""></asp:ListItem>
                                </asp:DropDownList>                          
                                    
                                <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" SelectCommand="SELECT * FROM Test6 Where DepartmentSub=@DepartmentSub ">
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="ddDepartmentID" Name="DepartmentSub" PropertyName="SelectedValue" />
                                    </SelectParameters>
                                </asp:SqlDataSource>                                                      
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("DepartmentSub") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="True" />
                    </Fields>   
                </asp:DetailsView>         
                
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseTestConnectionString %>" DeleteCommand="DELETE FROM [Test5] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Test5] ([Id], [Department], [DepartmentSub], [RequisitionDepartmentID]) VALUES (@Id, @Department, @DepartmentSub, @RequisitionDepartmentID)" SelectCommand="SELECT [Id], [Department], [DepartmentSub], [RequisitionDepartmentID] FROM [Test5]" UpdateCommand="UPDATE [Test5] SET [Department] = @Department, [DepartmentSub] = @DepartmentSub, [RequisitionDepartmentID] = @RequisitionDepartmentID WHERE [Id] = @Id">
                    <DeleteParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                        <asp:Parameter Name="Department" Type="String" />
                        <asp:Parameter Name="DepartmentSub" Type="String" />
                        <asp:Parameter Name="RequisitionDepartmentID" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="Department" Type="String" />
                        <asp:Parameter Name="DepartmentSub" Type="String" />
                        <asp:Parameter Name="RequisitionDepartmentID" Type="String" />
                        <asp:Parameter Name="Id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
                
            </div>   
    Aspx.cs:
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
    
                Label la = (Label)DetailsView1.FindControl("Label3");
                ViewState["l1"] = la.Text;
    
                Label la2 = (Label)DetailsView1.FindControl("Label4");
                ViewState["l2"] = la2.Text;
    
            }
            protected void DetailsView1_DataBound(object sender, EventArgs e)
            {            
                if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit)
                {
                    DropDownList ddl = (DropDownList)DetailsView1.FindControl("ddDepartmentID");
                    ddl.SelectedItem.Text=ViewState["l1"].ToString(); 
                    //ddl.SelectedValue= ViewState["abc"].ToString(); 
    
                    DropDownList ddl2 = (DropDownList)DetailsView1.FindControl("ddDepartmentSubID");
                    ddl2.SelectedItem.Text = ViewState["l2"].ToString();
                }
            }           
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 5, 2019 3:03 AM