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

  • Question

  • User260076833 posted

    Hello,

    I have two "nested" DropDownLists, i. e. the list items of the second one depend on the selection of the first one.

    <asp:SqlDataSource ID="src_Anlage" runat="server"
        ConnectionString='<%$ ConnectionStrings:StMGP %>'
        SelectCommand="SELECT idx,sym FROM slv_anlage ORDER BY sym;">
    </asp:SqlDataSource>
    
    <asp:SqlDataSource ID="src_Schluessel" runat="server"
        ConnectionString='<%$ ConnectionStrings:StMGP %>'
        SelectCommand="SELECT idx,sym,name_fancy FROM slv_schluessel WHERE (anlage=@anlage) ORDER BY sym;">
        <SelectParameters>
            <asp:Parameter Name="anlage"></asp:Parameter>
        </SelectParameters>
    </asp:SqlDataSource>
    
    <asp:ListView ID="lvw" ...>
        <EditItemTemplate>
    
            <asp:DropDownList ID="lst_Anlage" runat="server" DataSourceID="src_Anlage"
             AutoPostBack="True" DataValueField="idx" DataTextField="sym"
             OnSelectedIndexChanged="lst_Anlage_SelectedIndexChanged">
            </asp:DropDownList>
    
            <asp:DropDownList ID="lst_Schluessel" runat="server" DataSourceID="src_Schluessel"
                DataValueField="idx" DataTextField="sym" SelectedValue='<%# Bind("schluessel")%>'
            </asp:DropDownList>
    
        </EditItemTemplate>
    </asp:ListView>

    Here, the list "lst_Anlage" contains a list of master key systems. When such a master key system is selected,, the second list "lst_Schluessel" should be filled with all the keys that belong to the selected master key system.

    So in the selection event procedure (lst_Anlage_SelectedIndexChanged) I need to fill the second list "lst_Schluessel". I tried to do this by setting the SQL parameter "anlage":

    protected void lst_Anlage_SelectedIndexChanged(object snd, EventArgs evt)
    {
        DropDownList lst_Anlage = (DropDownList)snd;
    
        int idx_Anlage = Convert.ToInt32(lst_Anlage.SelectedValue);
        src_Schluessel.SelectParameters["anlage"].DefaultValue = idx_Anlage.ToString();
    }
    

    When I do this, I get the error message:

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

    Well, I had this problem some months ago, and I found a workaround by clearing and filling the list manually:

    lst_Schluessel.Items.Clear();
    ...
    while (reader.Read()) // traverse select results
    {
        ListItem i = new ListItem(...);
        lst_Schluessel.Items.Add(i);
    }
    

    I am working like this at several places now, but I still cannot believe that this should be the way to go.
    Is it really true that you cannot benefit from the power of SQL here and that you have to fill the list "by hand"?

    Hope someone can clarify this.

    Thanks
    Magnus

    Tuesday, October 17, 2017 4:31 AM

All replies

  • User-335504541 posted

    Hi Yeoman,

    Do you want to set a selected value to lst_Schluessel?

    The lst_Schluessel will not have any listitem until the lst_Anlage_SelectedIndexChanged is fired.

    So there will be a error if you set the SelectedValue in the page.

    You could try to set it in the lst_Anlage_SelectedIndexChanged.

    For example, you could try to use a hiddenfield to store the value you want to selected. Then set the value in lst_Anlage_SelectedIndexChanged.

    Here is the code:

    In aspx:

                                <td>
                                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("schluessel")%>' />
                                    <asp:DropDownList ID="lst_Anlage" runat="server" DataSourceID="src_Anlage"
                                        AutoPostBack="True" DataValueField="idx" DataTextField="sym"
                                        OnSelectedIndexChanged="lst_Anlage_SelectedIndexChanged">
                                    </asp:DropDownList>
    
                                    <asp:DropDownList ID="lst_Schluessel" runat="server" DataSourceID="src_Schluessel"
                                        DataValueField="idx" DataTextField="sym">
                                    </asp:DropDownList>
    
                                </td>

    In behind code:

            protected void lst_Anlage_SelectedIndexChanged(object snd, EventArgs evt)
            {
                DropDownList lst_Anlage = (DropDownList)snd;
                DropDownList lst_Schluessel = (DropDownList)lst_Anlage.Parent.FindControl("lst_Schluessel");
                HiddenField HiddenField1 = (HiddenField)lst_Anlage.Parent.FindControl("HiddenField1");
                var idx_Anlage =(lst_Anlage.SelectedValue);
                src_Schluessel.SelectParameters[0].DefaultValue = idx_Anlage;
                lst_Schluessel.SelectedValue = HiddenField1.Value;
    
    
            }

    Best Regards,

    Billy

    Wednesday, October 18, 2017 6:31 AM