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

Last post 02-17-2009 2:29 PM by CherylH. 11 replies.

Sort Posts:

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

    02-16-2009, 3:14 PM
    • Member
      18 point Member
    • CherylH
    • Member since 11-18-2008, 12:36 PM
    • Posts 51

    I am getting the following error in a listview that has an InsertItemTemplate and an EditItemTemplate where the code is used in both places:

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

    Line 213:              <td>
    Line 214:                <asp:RequiredFieldValidator ID="AgencyBizNameValidatorAdd" runat="server" ErrorMessage="*" ControlToValidate="ddlAgencyBizName" ValidationGroup="add"  Font-Size="Large" Font-Bold="True" />
    Line 215:                <asp:DropDownList ID="ddlAgencyBizName" DataSourceID="SqlDataSourceAgencyBiz" runat="server" DataTextField="AgencyBizName" DataValueField="agencyBizID" SelectedValue='<%# Bind("agencyBizID")%>'>
    Line 216:                </asp:DropDownList>
    Line 217:              </td>

    The SqlDataSource ...

    <asp:SqlDataSource ID="SqlDataSourceAgencyBiz" runat="server"

    ConnectionString="<%$ ConnectionStrings:dbExecutiveConnectionString %>"

    SelectCommand="SELECT 0 as agencyBizID, '' as agencyBizName FROM vFraudAgencyBiz UNION SELECT agencyBizID, Name as agencyBizName from vFraudAgencyBiz " SelectCommandType="Text">

    </asp:SqlDataSource>

     Any insight would be greatly appreciated.

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

    02-16-2009, 6:25 PM
    • Participant
      1,852 point Participant
    • webswapp
    • Member since 04-05-2006, 4:08 PM
    • Vancouver, BC, Canada
    • Posts 321

    I have examined this issue in a series of articles available through this link:

    http://webswapp.com/categories/ASPNET3/CascadingLists/ListView.aspx

    Regards,
    Phillip Williams,
    MCITP Database Developer 2008
    MCPD Web Developer
    MCTS Windows Communication Foundation
    http://www.webswapp.com
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-16-2009, 6:45 PM
    • Contributor
      2,162 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 446
    • Moderator

    This won't work in an InsertItemTemplate today the workaround is to set it manually in the ItemCreated event of the ListView.

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-16-2009, 7:00 PM
    • Participant
      1,852 point Participant
    • webswapp
    • Member since 04-05-2006, 4:08 PM
    • Vancouver, BC, Canada
    • Posts 321

    Hi David,

    The workaround that I have used in this ListView was to look at the problem from a different perspective.  I noticed that the problem was happening because the user was interested in saving all values of the cascading relation on the record using the 2-way databinding method BIND. 

    So what I tried is that instead of attempting to save all the values of the fields in the cascading list relation to one record, I would save only the primary key of the lowest item in the cascading relationship. 

    For example in the example of car make-model-color relationship, the color selection is not unique to the model and make selection.  Therefore you save all 3 values (make-model-color).  But if you were to change the underlying data such as the color record for a particular make and model has a different primary key than the color record for another make and model then all you need to BIND on the record is the primary key of the color (the lowest level in the cascading relationship).  The problem of selecting the unique color record becomes a problem of user interface handling events and no longer a problem of databinding data to a template control.

    Similarly in my demo,  I have demonstrated the case of (country-province-city) relationship.  Instead of saving on the address record the keys for all three selections, I save only the primary key of the city.  The process of populating the cascading dropdown lists does no longer interfere with the databidnding mechanism for the templated control.

     Regards,

    Regards,
    Phillip Williams,
    MCITP Database Developer 2008
    MCPD Web Developer
    MCTS Windows Communication Foundation
    http://www.webswapp.com
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-16-2009, 7:36 PM
    • Contributor
      2,162 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 446
    • Moderator

    Very nice website :). Still though this is a fundamental issue with databinding in general. When the DropDownList tries to DataBind itself it has no DataBinding context.

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-17-2009, 10:50 AM
    • Member
      18 point Member
    • CherylH
    • Member since 11-18-2008, 12:36 PM
    • Posts 51

    davidfowl:

    This won't work in an InsertItemTemplate today the workaround is to set it manually in the ItemCreated event of the ListView.

     David, I have the following code, is this what you were talking about? 

    Protected Sub ListView1_ItemInserting(ByVal sender As Object, ByVal e As ListViewInsertEventArgs)

      Dim ddlAgencyBizName As DropDownList = DirectCast(ListView1.InsertItem.FindControl("ddlAgencyBizName"), DropDownList)

      If ddlAgencyBizName IsNot Nothing Then

        e.Values("agencyBizID") = ddlAgencyBizName.SelectedValue

      End If

    End Sub

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

    02-17-2009, 12:35 PM
    • Contributor
      2,162 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 446
    • Moderator

    Yes but instead of the ItemInserting event ItemCreated event.

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-17-2009, 12:39 PM
    • Participant
      1,852 point Participant
    • webswapp
    • Member since 04-05-2006, 4:08 PM
    • Vancouver, BC, Canada
    • Posts 321

    Hi David,

    You are correct that during the Item insertion the databinding context is empty.

    The error message that the user reported happens during both the Edit and the Insert process. 

    The problem that I proposed a solution for was the problem that causes the reported error message upon both editing and insertion.

    In the demos on my website, I managed to solve the problem (for both editing and inserting a record that contains a cascading list) by changing the structure of the data that is involved in displaying a cascading list to the user.  The change is to make the records displayed on the lowest level of the cascading lists unique per every combination of the other members involved in the cascading relationship.  You can see it working on my website.  Therefore it works.

     

    Regards,
    Phillip Williams,
    MCITP Database Developer 2008
    MCPD Web Developer
    MCTS Windows Communication Foundation
    http://www.webswapp.com
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-17-2009, 12:50 PM
    • Contributor
      2,162 point Contributor
    • davidfowl
    • Member since 08-17-2008, 9:50 PM
    • Redmond
    • Posts 446
    • Moderator

    webswapp:

    Hi David,

    You are correct that during the Item insertion the databinding context is empty.

    The error message that the user reported happens during both the Edit and the Insert process. 

    The problem that I proposed a solution for was the problem that causes the reported error message upon both editing and insertion.

    In the demos on my website, I managed to solve the problem (for both editing and inserting a record that contains a cascading list) by changing the structure of the data that is involved in displaying a cascading list to the user.  The change is to make the records displayed on the lowest level of the cascading lists unique per every combination of the other members involved in the cascading relationship.  You can see it working on my website.  Therefore it works.

    You just found a workaround for the issue. This doesn't work because of the same reasons I stated before. It has nothing to do with the inserting event and has everything to do with the fact that DataBound controls will databind themselves. Don't get me wrong I'm not disputing the fact that you made it work.

    Hope this helps

    David Fowler
    SDE, ASP.NET Team, Microsoft
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-17-2009, 12:53 PM
    • Member
      18 point Member
    • CherylH
    • Member since 11-18-2008, 12:36 PM
    • Posts 51

    davidfowl:

    Yes but instead of the ItemInserting event ItemCreated event.

    I added the following and the page no longer bombs when opening the Insert Item in the listview.

    Protected Sub ListView1_ItemCreated(ByVal sender As Object, ByVal e As ListViewItemEventArgs)

    If e.Item.ItemType = ListViewItemType.InsertItem Then

      Dim ddlAgencyBizName As DropDownList = DirectCast(ListView1.InsertItem.FindControl("ddlAgencyBizName"), DropDownList)

      If ddlAgencyBizName IsNot Nothing AndAlso ddlAgencyBizName.Items.Count = 0 Then

        strSQL = "SELECT 0 as agencyBizID, '' as agencyBizName FROM vFraudAgencyBiz UNION SELECT agencyBizID, agencyBizName from vFraudAgencyBiz"

        objDS.Clear()

        myDA = New SqlDataAdapter(strSQL, myConn) ' ## pass sql to server

        myDA.Fill(objDS) ' ## get the data

        ddlAgencyBizName.DataSource = objDS

        ddlAgencyBizName.DataBind()

      End If

    End If

    End Sub

    It still bombs on the edit. Here is the error message: 

    'ddlAgencyBizName' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    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.ArgumentOutOfRangeException: 'ddlAgencyBizName' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Is there an edit counterpart that I need to add?

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

    02-17-2009, 1:01 PM
    • Participant
      1,852 point Participant
    • webswapp
    • Member since 04-05-2006, 4:08 PM
    • Vancouver, BC, Canada
    • Posts 321
    In the demos that I presented for the ASP.NET 3 version on my website, I did not propose a technical workaround for circumventing the consequences of the BIND (2-way databinding) mechanism.I proposed a new architectural perspective to deal with the underlying data instead of tinkering with handling the UI events against the existing data.

    However, the demos that I have on my website for the ASP.NET 2.0 version were workarounds for dealing with the BIND issue using the available events and non-unique record data on the lowest level of the cascading list relationship.

     

    Regards,
    Phillip Williams,
    MCITP Database Developer 2008
    MCPD Web Developer
    MCTS Windows Communication Foundation
    http://www.webswapp.com
  • Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    02-17-2009, 2:29 PM
    Answer
    • Member
      18 point Member
    • CherylH
    • Member since 11-18-2008, 12:36 PM
    • Posts 51

    Solution:

    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource2" OnSorting="ListViewSorting" DataKeyNames="contactID" OnItemCommand="ItemCommand" InsertItemPosition="LastItem" OnItemInserting="ListView1_ItemInserting" OnItemCreated="ListView1_ItemCreated">

    <EditItemTemplate>

    <tr>

      <td valign="top">Agency/Business:</td>

      <td>

        <asp:DropDownList ID="ddlAgencyBizName" runat="server" DataTextField="AgencyBizName" DataSourceID="SqlDataSourceAgencyBiz" DataValueField="agencyBizID" SelectedValue='<%# Bind("agencyBizID")%>'>

        </asp:DropDownList>

      </td>

    </tr>

    </EditItemTemplate>

    <InsertItemTemplate>

    <tr>

      <td valign="top">Agency/Business:</td>

      <td>

        <asp:DropDownList ID="ddlAgencyBizName" runat="server" AppendDataBoundItems="true" AutoPostBack="true" DataTextField="AgencyBizName" DataValueField="agencyBizID">

        </asp:DropDownList>

      </td>

    </tr>

    </InsertItemTemplate>

    </asp:ListView>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"

    ConnectionString="<%$ ConnectionStrings:dbExecutiveConnectionString %>"

    SelectCommand="proc_GetContacts" SelectCommandType="StoredProcedure">

    </asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSourceAgencyBiz" runat="server"

    ConnectionString="<%$ ConnectionStrings:dbExecutiveConnectionString %>"

    SelectCommand="SELECT 0 as agencyBizID, '' as agencyBizName FROM vAgencyBiz UNION SELECT agencyBizID, agencyBizName from vAgencyBiz " SelectCommandType="Text">

    </asp:SqlDataSource>

    Added the following to the code behind:

    Protected Sub ListView1_ItemCreated(ByVal sender As Object, ByVal e As ListViewItemEventArgs)

      If e.Item.ItemType = ListViewItemType.InsertItem Then

      Dim ddlAgencyBizName As DropDownList = DirectCast(ListView1.InsertItem.FindControl("ddlAgencyBizName"), DropDownList)

        If ddlAgencyBizName IsNot Nothing AndAlso ddlAgencyBizName.Items.Count = 0 Then

          strSQL = "SELECT 0 as agencyBizID, ' Please make a selection ' as agencyBizName FROM vAgencyBiz UNION SELECT agencyBizID, agencyBizName from vAgencyBiz"

          objDS.Clear()

          myDA = New SqlDataAdapter(strSQL, myConn) ' ## pass sql to server

          myDA.Fill(objDS) ' ## get the data

          ddlAgencyBizName.DataSource = objDS

          ddlAgencyBizName.DataBind()

        End If

      End If

    End Sub

    ********************************************

    Basically, the dropdownlist in InsertItemTemplate needed OnItemCreated="ListView1_ItemCreated" added to <asp:listview1> with the dropdownlist being populated in the code behind. The dropdownlist in EditItemTemplate needed to have the datasourceID specified and populated on the front side for lack of a better way to explain it - I'm not very techy, sorry.
Page 1 of 1 (12 items)