Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a DropDownList in ListView InsertItemTemplate throwing an error

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>
like image 655
CodeRedick Avatar asked Jan 22 '09 18:01

CodeRedick


2 Answers

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?

like image 70
ruffone Avatar answered Sep 27 '22 18:09

ruffone


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.

like image 21
stefann Avatar answered Sep 27 '22 19:09

stefann