Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Error message when you use nested data controls that have different data sources in ASP.NET 2.0 or in later versions: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control"


View products that this article applies to.

Symptoms

You have a computer that is running Microsoft ASP.NET 2.0 or later versions. When you use nested data controls that have different data sources, you intermittently receive the following error message:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
You receive the error message in one of the following situations.

Situation 1

You use a nested data presentation control to call the DataBind method. The data presentation control is bound to a data source. The following is a sample code that can be used to call the DataBind method:
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
        DataKeyNames="EmployeeID" DataSourceID="SqlDataSource2" Height="50px" Width="125px"
        AutoGenerateEditButton="True">
		<Fields>
            <asp:TemplateField HeaderText="Department">
                <ItemTemplate>
                    <%#Eval("DepartmentId") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddrDepartment" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="DepartmentID" SelectedValue='<%#Bind("DepartmentId") %>' onselectedindexchanged="ddrDepartment_SelectedIndexChanged" AutoPostBack="True">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                        SelectCommand="SELECT DepartmentID, Name FROM HumanResources.Department"></asp:SqlDataSource>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
        SelectCommand="SELECT EmployeeID, DepartmentId, Title FROM HumanResources.Employee"
        UpdateCommand="UPDATE HumanResources.Employee SET DepartmentId = @DepartmentId, Title = @Title WHERE (EmployeeID = @employeeid)">
        <UpdateParameters>
            <asp:Parameter Name="DepartmentId" />
            <asp:Parameter Name="Title" />
            <asp:Parameter Name="employeeid" />
        </UpdateParameters>
</asp:SqlDataSource>
Microsoft Visual C# code
protected void ddrDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            	DropDownList ddr = (DropDownList)DetailsView1.Rows[1].FindControl("ddrDepartment");
             ddr.DataBind();
        }
Microsoft Visual Basic .NET code
Protected Sub ddrDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 
    Dim ddr As DropDownList = DirectCast(DetailsView1.Rows(1).FindControl("ddrDepartment"), DropDownList) 
    ddr.DataBind() 
End Sub

Situation 2

You use an ASP.NET control to retrieve the title of a Web page. The control does not have the DataSource property. The following is a sample code that can be used to retrieve titles of Web pages:
<asp:TextBox id=�txtBox� runat=�server�  Text=�<%#Eval(�Title�)%>�/>
Microsoft Visual C# code
txtBox.DataBind();
Microsoft Visual Basic .NET code
txtBox.DataBind()

↑ Back to the top


Cause

Cause of problem in situation 1

When the DropDownList instance calls the DataBind method, the SelectedValue property tries to obtain the value that is in the data binding context of the DetailsView control. However, the value is not included in the data binding context of the DetailsView control. Therefore, the error occurs.

Cause of problem in situation 2

The ASP.NET controls that do not have the DataSource property can be used only in the context of data presentation controls. Examples of data presentation controls include the DetailsView control and the GridView control.

↑ Back to the top


Workaround

Workaround for problem in situation 1

Delete the source code in the SelectedIndexChanged event of the DropDownList control. When you select an item from the DropDownList control and the item is in edit mode, the item is automatically bound to the SelectedValue property. Then, the value of the item is automatically updated in the DataTable object.

Note You cannot find the SelectedValue property by using the IntelliSense feature. However, you can write the property manually.

Workaround for problem in situation 2

Use the following code to work around this problem:
	<asp:TextBox id="txtBox" runat="server" Text='<%#Page.Title%>'/>

↑ Back to the top


Keywords: KB978215, kbsurveynew, kbinfo, kbexpertiseadvanced

↑ Back to the top

Article Info
Article ID : 978215
Revision : 1
Created on : 12/22/2009
Published on : 12/22/2009
Exists online : False
Views : 376