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# codeprotected void ddrDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddr = (DropDownList)DetailsView1.Rows[1].FindControl("ddrDepartment");
ddr.DataBind();
}
Microsoft Visual Basic .NET codeProtected 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# codeMicrosoft Visual Basic .NET code