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

    Question

  • I have the following structure 2 combos, the 2nd dependant on the first - and when I change the first combo the pages refreshes with error

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

    Which i assume has something to do with the 2nd combo rebinding and breaking the binding of the FormView. Have seen this prob around but can find a solution..

    Any ideas?

    Nik


    <
    asp:FormView ID="frm" runat="server" DataSourceID="SqlDataSource1" DefaultMode="Edit">

    <EditItemTemplate>

    <tr><td>Level1</td><td><asp:DropDownList runat="server" id="cmbEntity" DataSourceID="sqlLevel1" DataTextField="Level1" DataValueField="Level1" SelectedValue='<%# Bind("Level1") %>' AutoPostBack="true"></asp:DropDownList></td></tr>

    <tr><td>Level2</td><td><asp:DropDownList runat="server" id="cmbLevel2" DataSourceID="sqlLevel2" DataTextField="Level2" DataValueField="Level2" SelectedValue='<%# Bind("Level2") %>'></asp:DropDownList></td></tr>

    <
    asp:SqlDataSource ID="sqlLevel1" runat="server" ConnectionString="<%$ ConnectionStrings:Database %>" SelectCommand="SELECT Level1 FROM Level1"></asp:SqlDataSource>

    <
    asp:SqlDataSource ID="sqlLevel2" runat="server" ConnectionString="<%$ ConnectionStrings:Database%>" SelectCommand="SELECT Level2 FROM Level2 WHERE Level1 = @Level1">

    <SelectParameters>

    <asp:ControlParameter Name="Level1" ControlID="cmbLevel1" PropertyName="SelectedValue" />

    </SelectParameters>

    </asp:SqlDataSource>

    </EditItemTemplate>

    </asp:FormView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database%>" UpdateCommand="z" />

    Thursday, November 24, 2005 3:59 PM

Answers

All replies

  • Hi!

     Thanks for asking! I'm a member of the ASP.NET team, and was just popping over here to see what was going on. The best place to ask ASP.NET questions is over on the ASP.NET forums at http://www.asp.net/welcome.aspx?tabindex=1&tabid=39

    HTH,

    PEte

    Monday, November 28, 2005 6:22 PM
  • Saturday, December 17, 2005 7:27 PM
  • Hi,

     

    I've found a simple solution to Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. Suppose you have these two dropdownlist controls each one into an edittemplate of a detailsview (the first with autopostback):

    <EditItemTemplate>

    <asp:DropDownList ID="drpCats" runat="server" AutoPostBack="True" DataSourceID="sqlDSCats" DataTextField="Categoria" DataValueField="CategoriaID" SelectedValue='<%# Bind("CategoriaID") %>'>

    </asp:DropDownList>

    <asp:SqlDataSource ID="sqlDSCats" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2000_FinancialApp %>"

    SelectCommand="SELECT [CategoriaID], [Categoria] FROM [tbl_Categorias]"></asp:SqlDataSource>

    </EditItemTemplate>

    <EditItemTemplate>

    <asp:DropDownList ID="drpProds" runat="server" DataSourceID="sqlDSProds" DataTextField="Producto" DataValueField="ProductoID" SelectedValue='<%# xx(DataBinder.Eval(Container.DataItem,"ProductoID")) %>' AppendDataBoundItems="False"></asp:DropDownList>

    <asp:SqlDataSource ID="sqlDSProds" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2000_FinancialApp %>"

    SelectCommand="SELECT [ProductoID], [Producto] FROM [tbl_Productos] WHERE ([CategoriaID] = @CategoriaID)">

    <SelectParameters>

    <asp:ControlParameter ControlID="drpCats" Name="CategoriaID" PropertyName="SelectedValue"

    Type="Int64"  />

    </SelectParameters>

    </asp:SqlDataSource>

    </EditItemTemplate>

    1. Note in the second dropdownlist the SelectedValue Property (<%#  xx(.....)%>). "xx" is a function like this:

    function xx(ByVal a) as string

          return a

    end function

    2. Taking into account that the eval method is not for two way databinding in edit and insert templates, the DetailsView control doesn't understand that the second dropdownlist is a member field for update. In this sense, you need to manually add an entry to the e.values dictionary into the ItemUpdating event.

    Protected Sub DetailsView1_ItemUpdating(.......)

    Dim d1 As DropDownList = DetailsView1.FindControl("drpProds")

    e.NewValues("ProductoID") = d1.SelectedValue

    End Sub

     

    Thats all ! simple ! I've found other "over-coded" solutions but takes so long for a simple problem.

     

    CFC2006

     

     

    Thursday, July 06, 2006 5:25 AM
  • I'm being bewildered by the same problem for several days.

    Would you pls give the detailed description and the source code of this part:

    1. Note in the second dropdownlist the SelectedValue Property (<%#  xx(.....)%>). "xx" is a function like this:

    function xx(ByVal a) as string

          return a

    end function

     

    Thank you Very much!

    Sunday, July 16, 2006 2:21 PM
  •  

    As per the top few posts, this group is for problems with installing and registering the express products, for web development problems you need to post at http://forums.asp.net.

     

    Sunday, July 16, 2006 10:22 PM