Reputation: 1894
I'm trying to update the contents of one DropDownList
based on the value of 3 other DropDownLists
. I have 2 tables, one containing Projects
and one containing SubProjects
. These 2 have 3 matching Properties:
I want to assign SubProjects
to Projects
using these matching criteria. Thus my SubProject
Adapter has a method that wetches a view containing this data. In the view I try to use following code:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DetailsView ID="DetailsViewProject" runat="server" AutoGenerateRows="False" DataKeyNames="Guid" DataSourceID="ObjectDataSourceProject" DefaultMode="Insert" Height="50px" Width="125px" OnItemInserted="DetailsViewProject_ItemInserted">
<Fields>
<asp:TemplateField HeaderText="Name">
<InsertItemTemplate>
<asp:TextBox ID="TextBoxName" Width="245px" runat="server" Text='<%# Bind("Name") %>' MaxLength="100" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" ControlToValidate="TextBoxName" runat="server" ErrorMessage="Please specifiy a Name">*</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtenderName" runat="server" TargetControlID="RequiredFieldValidatorName" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<InsertItemTemplate>
<asp:DropDownList ID="lstCountries" Width="250px" runat="server" DataSourceID="ObjectDataSourceCountries" DataTextField="Name" DataValueField="ID" SelectedValue='<%#Bind("CountryID") %>' AutoPostBack="True"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Organisation Unit">
<InsertItemTemplate>
<asp:DropDownList ID="lstOEs" Width="250px" runat="server" DataSourceID="ObjectDataSourceOEs" DataTextField="Name" DataValueField="ID" SelectedValue='<%#Bind("OrganisationUnitID") %>' OnDataBinding="lstOEs_DataBound" AutoPostBack="True"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year">
<InsertItemTemplate>
<asp:DropDownList ID="lstProjectYears" Width="250px" runat="server" DataSourceID="SqlDataSourceProjectYears" DataTextField="ProjectYear" DataValueField="ProjectYear" SelectedValue='<%#Bind("ProjectYear") %>' OnDataBinding="lstProjectYears_DataBound" AutoPostBack="True"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project">
<InsertItemTemplate>
<asp:DropDownList ID="lstProjectOverall" Width="250px" runat="server" DataTextField="ProjectName" DataSourceID="ObjectDataSourcePOs" DataValueField="ID" SelectedValue='<%#Bind("ProjectOverallID")%>'></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowCancelButton="False" ShowInsertButton="True" InsertText="Insert" />
</Fields>
</asp:DetailsView>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:ObjectDataSource ID="ObjectDataSourceProject" runat="server" ...and so on
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSourceOEs" runat="server" ...and so on
</asp:ObjectDataSource>
<-- here -->
<asp:ObjectDataSource ID="ObjectDataSourcePOs" runat="server"
SelectMethod="GetProjectsOverallByParameters" TypeName="Projects">
<SelectParameters>
<asp:ControlParameter ControlID="DetailsViewProject$lstProjectYears" name="ProjectYear" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstOEs" name="OrganisationUnitID" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstCountries" name="CountryID" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSourceCountries" runat="server" OldValuesParameterFormatString="{0}" SelectMethod="GetCountries" TypeName="Countries"></asp:ObjectDataSource>
<asp:SqlDataSource ID="SqlDataSourceProjectYears" runat="server" ...and so on
</asp:SqlDataSource>
</asp:Content>
But I'm getting this error:
System.InvalidOperationException: 'Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.'
Why am I getting this error and how can I solve this? How can I refetch the data from the adapter using either code behind or the aspnet update mechanism?
Upvotes: 0
Views: 55
Reputation: 1894
I solved the issue:
The problem was, that Project
scheme and ProjectOverall
scheme didn't match.
I changed the adapter and added an access method for ProjectOverall
.
SELECT ID, CountryID, OrganisationUnitID, ProjectYear, ProjectName
FROM wc_ProjectsOverall
WHERE (CountryID = @CountryID) AND (OrganisationUnitID = @OrganisationUnitID) AND (ProjectYear = @ProjectYear)
In ProjectOverall.cs
:
[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Select, false)]
public WebControlling.ProjectsOverallDataTable GetProjectsOverallByAttributes(Guid CountryID, Guid OrganisationUnitID, Int32 ProjectYear)
{
if (CountryID == null || OrganisationUnitID == null || ProjectYear == 0)
return new WebControlling.ProjectsOverallDataTable();
return Adapter.GetProjectsByAttribute(CountryID, OrganisationUnitID, ProjectYear);
}
In the view ProjectAdd.aspx
:
<asp:TemplateField HeaderText="Project">
<InsertItemTemplate>
<asp:DropDownList ID="lstProjectOverall" Width="250px" runat="server" DataTextField="ProjectName" DataSourceID="ObjectDataSourcePOs" DataValueField="ID" AppendDataBoundItems="False"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:ObjectDataSource ID="ObjectDataSourceProject" runat="server" InsertMethod="AddProject" OldValuesParameterFormatString="{0}" SelectMethod="GetProjects" TypeName="Projects" OnInserting="ObjectDataSourceProject_Inserting" >
<InsertParameters>
<asp:Parameter Name="CountryID" Type="Object" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Objective" Type="String" />
<asp:Parameter Name="ObjectiveQuantitativ" Type="String"/>
<asp:Parameter Name="ProjectYear" Type="Int32" />
<asp:Parameter Name="BaseCampaign" Type="Boolean" />
<asp:Parameter Name="ManagerID" Type="Object" />
<asp:Parameter Name="OrganisationUnitID" Type="Object" />
<%-- This parameter is different because the ProjectOverall ID is not part of the Project model, thus we insert the DropDownList value --%>
<asp:ControlParameter ControlID="DetailsViewProject$lstProjectOverall" name="ID" PropertyName="SelectedValue"/>
</InsertParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSourcePOs" runat="server"
SelectMethod="GetProjectsOverallByAttributes" TypeName="ProjectsOverall">
<SelectParameters>
<asp:ControlParameter ControlID="DetailsViewProject$lstProjectYears" name="ProjectYear" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstOEs" name="OrganisationUnitID" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstCountries" name="CountryID" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:ObjectDataSource>
And that solved my issue.
Upvotes: 0
Reputation: 309
you have to use property AppendDataBoundItems="True|False" when you have to put existing Item as it is.
Upvotes: 1