locked
Child Gridview, DropDownList DataSource Error RRS feed

  • Question

  • User-762312836 posted

    Hi all

    I'm getting this error: DropDownList binding get Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control exception

    What I'm trying to do is:

    • Use 'Edit' functionality (using CommandArgument and CommandName in ItemTemplates)
    • Present controls (a dropdownlist and textboxes).
    • NOTE: so far so good, that works.
    • Rather than add list items in my markup, I want to use a datasource for my dropdown list so the correct values are passed to the database on submission. Whenever I add the code shown below that adds the DataSourceID attribute etc. to my dropdownlist, I get: DropDownList binding get Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control exception

    Markup:

                <asp:GridView ID="gridview_Child" CssClass="grid" runat="server" AutoGenerateColumns="false" ShowFooter="true" DataKeyNames="ResourceID" OnRowCommand="gridview_Child_RowCommand">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lbEdit" runat="server" CommandArgument='<%# Eval("ResourceID") %>' CommandName="EditRow">Edit</asp:LinkButton>
                                <asp:LinkButton ID="lbDelete" runat="server" CommandArgument='<%# Eval("ResourceID") %>' CommandName="DeleteRow">Delete</asp:LinkButton>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="lbUpdate" runat="server" CommandArgument='<%# Eval("ResourceID")%>' CommandName="UpdateRow">Update</asp:LinkButton>
                                <asp:LinkButton ID="lbCancel" runat="server" CommandArgument='<%# Eval("ResourceID") %>' CommandName="CancelUpdate">Cancel</asp:LinkButton>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ResourceID" InsertVisible="false">
                            <EditItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("ResourceID")%>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("ResourceID")%>'></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:LinkButton ID="lbInsert" runat="server" CommandName="InsertRow">Insert</asp:LinkButton>
                            </FooterTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="ResourceTypeID">
                            <EditItemTemplate>
                                <asp:DropDownList
                                    ID="ddlResourceTypeIDEdit"
                                    runat="server"
                                    CssClass="gridControl"
                                    DataSourceID="ODS_ResourceTypes"
    DataTextField="ResourceName"
    DataValueField="ResourceTypeID"
    SelectedValue='<%# Bind("ResourceTypeID") %>'> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("ResourceTypeID")%>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="ddlResourceTypeID" runat="server" CssClass="gridControl"> <asp:ListItem>Select Resource</asp:ListItem> <asp:ListItem>FU</asp:ListItem> <asp:ListItem>WI</asp:ListItem> <asp:ListItem>DI</asp:ListItem> <asp:ListItem>GL</asp:ListItem> <asp:ListItem>MSG</asp:ListItem> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Resource"> <EditItemTemplate> <asp:TextBox ID="tbResourceEdit" runat="server" Text='<%# Bind("Resource")%>' CssClass="gridControl"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("Resource")%>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="tbResource" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ContentID"> <EditItemTemplate> <asp:TextBox ID="tbContentIDEdit" runat="server" Text='<%# Bind("ContentID")%>' CssClass="gridControl"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("ContentID")%>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="tbContentID" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ResourceName"> <EditItemTemplate> <asp:TextBox ID="tbResourceNameEdit" runat="server" Text='<%# Bind("ResourceName")%>' CssClass="gridControl"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("ResourceName")%>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="tbResourceName" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:ObjectDataSource ID="ODS_ResourceTypes" runat="server" TypeName="Aurora.BLL.AdminBiz" SelectMethod="GetResourceTypes"></asp:ObjectDataSource>

    RowCommand Event handler for linkbuttons:

            protected void gridview_Child_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                AdminBiz objAdmin = new AdminBiz(DepartmentMgmt.DetermineDepartment(dropdown_AreaSelect.Text));
    
                if (e.CommandName == "EditRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                    gridview_Child.EditIndex = rowIndex;
                    BindData_Child();
                }
                else if (e.CommandName == "DeleteRow")
                {
                    objAdmin.DeleteResource(Convert.ToInt32(e.CommandArgument));
                    BindData_Child();
                }
                else if (e.CommandName == "CancelUpdate")
                {
                    gridview_Child.EditIndex = -1;
                    BindData_Child();
                }
                else if (e.CommandName == "UpdateRow")
                {
                    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
    
                    dsResponses.tblResourceDataTable dt = (dsResponses.tblResourceDataTable)Session["resourceTable"];
    
                    GridViewRow row = gridview_Child.Rows[rowIndex];
                    dt[row.DataItemIndex].ResourceTypeID = ((DropDownList)row.FindControl("ddlResourceTypeIDEdit")).SelectedValue;
                    dt[row.DataItemIndex].Resource = ((TextBox)row.FindControl("tbResourceEdit")).Text;
                    dt[row.DataItemIndex].ContentID = Convert.ToInt32(((TextBox)row.FindControl("tbContentIDEdit")).Text);
    
                    dsResponses.tblResourceRow resRow = dt[row.DataItemIndex];
    
                    try
                    {
                        objAdmin.UpdateResourceTable(resRow);
                        gridview_Child.EditIndex = -1;
                        BindData_Child();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print(ex.Message);
                    }
    
                }
                else if (e.CommandName == "InsertRow")
                {
                    ds = new dsResponses();
                    GridViewRow gridRow = gridview_Child.FooterRow;
    
                    dsResponses.tblResourceRow resRow = ds.tblResource.NewtblResourceRow();
    
                    resRow.ResourceTypeID = ((DropDownList)gridRow.FindControl("ddlResourceTypeID")).SelectedValue;
                    resRow.Resource = ((TextBox)gridRow.FindControl("tbResource")).Text;
                    resRow.ContentID = Convert.ToInt32(((TextBox)gridRow.FindControl("tbContentID")).Text);
    
                    try
                    {
                        objAdmin.InsertResource(resRow);
                        gridview_Child.EditIndex = -1;
                        BindData_Child();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print(ex.Message);
                    }
                }
            }

    I've seen enough forum posts relation to the same error to suggest it's to do with the 'Bind' part of my markup against my 'EditTemplate' dropdownlist. But, I can't find anything that offers a solution to my particular problem.

    Thanks

    Tuesday, March 13, 2018 12:34 PM

All replies

  • User-1838255255 posted

    Hi jmr,

    After reading your description, the cause here is very simple, there is a <%# Bind...%> databinding expression in Dropdownlist here:

    SelectedValue='<%# Bind("ResourceTypeID") %>'

    When you call DropDownList.DatdBind, this expression is also evaluated, and since this time it is out of the container databinding context, the exception will get raised.

    For such scenario, we can consider the following resolutions:

    1. For inner nested controls, instead of databinding, we use other methods to populate its items.
      For example, we can use a for loop to add items into the dropdownlist instead of databinding.
      Thus, the <%# ...%> expression will not be evaluated.
    2.  We can also perform change on the container control.
      Instead of <%# %> expression, we can manually use some event such as "RowDataBound" to populate data item to the certain inner control.
      And when perform updating/inserting, manually extract the values from controls in the proper event(such as ItemUpdating or ItemInserting...).

    Best Regards,

    Eric Du 

    Wednesday, March 14, 2018 6:47 AM