6

I've got a ListView which binds to a LinqDataSource and displays selected locations. The insert item Contains a dropdownlist that pulls from another LinqDataSource to give all the unselected locations.

The problem is that I get the following error when loading the page:

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

I'm doing a very similar setup in another page of the website, and it isn't giving us this error so I'm pretty confused. I know I can work around this by not binding, manually finding the control and getting the value, but this should work and I don't understand why it isn't.

Any thoughts?

The better part of the source code is below.

<asp:LinqDataSource ID="ldsLocations" runat="server" 
    ContextTypeName="ClearviewInterface.ESLinqDataContext" EnableDelete="true" EnableInsert="true"
    OnInserting="ldsLocations_Inserting" OnDeleting="ldsLocations_Deleting" 
    TableName="crmLocations" OrderBy="addr1" OnSelecting="ldsLocations_Selecting" />

<asp:LinqDataSource ID="ldsFreeLocations" runat="server" 
    ContextTypeName="ClearviewInterface.ESLinqDataContext" OrderBy="addr1" 
    TableName="v_CVLocations" OnSelecting="ldsFreeLocations_Selecting" />

<asp:ListView ID="lvLocations" DataSourceID="ldsLocations" DataKeyNames="ID" InsertItemPosition="LastItem" runat="server" >

<InsertItemTemplate>
        <tr>
            <td colspan="6"><hr /></td>
        </tr>
        <tr>
            <td colspan="2">

                <asp:DropDownList    ID="ddlFreeLocations" DataSourceID="ldsFreeLocations" DataTextField="addr1" 
                                        DataValueField="record" MarkFirstMatch="true" SelectedValue='<%# Bind("record") %>' 
                                        runat="server" />
            </td>
            <td><asp:ImageButton ID="btnAdd" CommandName="Insert" SkinID="Insert" runat="server" /></td>
        </tr>

    </InsertItemTemplate>
CodeRedick
  • 7,346
  • 7
  • 46
  • 72
  • Bind in Dropdown's SelectedValue dont works when Dropdownlist is in Listview... as you said you have same setup in another website which is working... have you placed dropdown in Listview only or in formview or gridview??? i also searched lot of blogs as i am getting same problem. but i found only one soln. tht use Eval to display value and to "insert" or "Update" do code in ItemInserting and itemUpdating – Radhi Dec 31 '09 at 06:04
  • if you find any soln. to do binding in dropdown inside listview... please provide it to me too... – Radhi Dec 31 '09 at 06:05

7 Answers7

5

As explained on Joteke's Blog here is a quoted solution.

Are you using <%#Eval("field")%> expression to bind the control? If you change that to <%#DataBinder.Eval(Container.DataItem,"field")%>, does that seem to fix the error?

Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
ruffone
  • 103
  • 2
  • 7
  • 1
    YES! This was my problem where I used 'eval(666)' to construct a sub control and a null value throws a wobbley. I used the `<%#DataBinder.Eval(Container.DataItem,"field")%>,` and it solved the problem because then my sub control handled the null and did not fall over like a drunken moneky. PS I updated your answer because link only answers are bad... the stuff behind the link might dissapear one day. – Piotr Kula Sep 15 '14 at 10:36
2

Take a look at this thread http://forums.asp.net/t/1187425.aspx

Basically you can't use Bind syntax because controls inside InsertItemTemplate are "one-way" only. Use code behind to get the values.

stefann
  • 1,045
  • 10
  • 14
1

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 DetailsView 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...).

source: http://blogs.msdn.com/b/stcheng/archive/2009/01/15/aspnet-system-invalidoperationexception-databinding-methods-such-as-eval-xpath-and-bind-can-only-be-used-in-the-context-of-a-databound-control-error-in-nested-databinding-scenario.aspx

Amin
  • 99
  • 4
1

this will work

 protected void lstCompContctPerson_ItemCreated(object sender, RadListViewItemEventArgs e)
        {

        if ((e.Item != null) && (e.Item.ItemType == RadListViewItemType.InsertItem))
        {
            System.Web.UI.Control ddlCompNames = e.Item.FindControl("ddlCompNames");

           (ddlCompNames as RadDropDownList).DataSource = (from _company in objshortcut.GetAllCompanies()
            select new
            {
                  CompanyID = _company.CompanyID,
                  CompContctName = _company.CompanyName
             }).Distinct();

        }
    }
slecorne
  • 1,720
  • 1
  • 11
  • 14
0

I've managed it. I have a Formview with a SqlDataSource and on the form view there is a dropdownlist iwth a SQLdataSource.

When a new record is requested I re-issue the select commands for both data sources - the dropdownlist source provides a list of options while the formview datasource returns nothing.

Then I use DataBind() on both the formview and the dropdownlist and all is well.

I'm new to ASP.Net but this is working for me and allowing me to do what I want - use Bind on the dropdownlist and the textboxes on the formview

I hope this helps Nathan

nathj07
  • 685
  • 1
  • 11
  • 23
0

With #Eval it may be worth using DataBinder.Eval() to ensure the binding happens at the right time.

nathj07
  • 685
  • 1
  • 11
  • 23
0

I've had a similar issue. Try to use #Eval instead of if you aren't updating the value of that field.

Gthompson83
  • 1,099
  • 1
  • 8
  • 18