<p>I have a form view, in the edit template I have two drop downs. 
Drop down 1 is explicitly set with a list of allowed values. It is also set to autopostback.
Drop down 2 is databound to an objectdatasource, this objectdatasource uses the first dropdown as one of it's parameters. (The idea is that drop down 1 limits what is shown in drop down 2)</p>

<p>On the first view of the edit template for an item it works fine. But if drop down 1 has a different item selected it post back and generates an error </p>

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

<p>Here is the drop down list #2:</p>

<pre><code>&lt;asp:DropDownList ID="ProjectList" runat="server" SelectedValue='&lt;%# Bind("ConnectToProject_ID","{0:D}") %&gt;' DataSourceID="MasterProjectsDataSource2" DataTextField="Name" DataValueField="ID" AppendDataBoundItems="true"&gt;
      &lt;asp:ListItem Value="0" Text="{No Master Project}" Selected="True" /&gt;
&lt;/asp:DropDownList&gt;
</code></pre>

<p>And here is the MasterProjectDataSource2:</p>

<pre><code>&lt;asp:ObjectDataSource ID="MasterProjectsDataSource2" runat="server" 
            SelectMethod="GetMasterProjectList" TypeName="WebWorxData.Project" &gt;
            &lt;SelectParameters&gt;
                &lt;asp:ControlParameter ControlID="RPMTypeList" Name="RPMType_ID" 
                    PropertyName="SelectedValue" Type="Int32" /&gt;
            &lt;/SelectParameters&gt;
        &lt;/asp:ObjectDataSource&gt;
</code></pre>

<p>Any help on how to get this to work would be greatly appriciated.</p>

[{'answer_id': 22797, 'author': 'Joel Meador', 'author_id': 1976, 'author_profile': 'https://Stackoverflow.com/users/1976', 'pm_score': 0, 'selected': False, 'text': "<p>Sounds like the controls aren't being databound properly after the postback.</p>\n\n<p>Are you databinding the first dropdown in the page or in the codebehind?\nIf codebehind, are you doing it in on_init or on_load every time?</p>\n\n<p>There might be an issue of the SelectedValue of the second drop down being set to a non-existent item after the postback.</p>\n"}, {'answer_id': 25612, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 0, 'selected': False, 'text': "<p>Unless your 2nd dropdown is in a databound control (say, a Repeater) - I'm not sure what you're trying to bind SelectedValue to. Apparently, neither is .NET - since that's probably where the error is occurring. </p>\n\n<p>Where's Connect_ToProjectId supposed to come from?</p>\n"}, {'answer_id': 183587, 'author': 'craigmoliver', 'author_id': 12252, 'author_profile': 'https://Stackoverflow.com/users/12252', 'pm_score': 2, 'selected': False, 'text': '<p>I had a similar problem with bound dropdownlists in a FormView.  I worked around it by setting the selected value manually in the formview\'s "OnDataBound".  </p>\n\n<p>(don\'t know where you get ConnectToProject_ID from)</p>\n\n<pre><code>FormView fv = (FormView)sender;\nDropDownList ddl = (DropDownList)fv.FindControl("ProjectList");\nddl.SelectedValue = String.Format("{0:D}", ConnectToProject_ID);\n</code></pre>\n\n<p>When you ready to save, use the "OnItemInserting" event:</p>\n\n<pre><code>FormView fv = (FormView)sender;\nDropDownList ddl = (DropDownList)fv.FindControl("ProjectList");\ne.Values["ConnectToProject_ID"] = ddl.SelectedValue;\n</code></pre>\n\n<p>or "OnItemUpdating"</p>\n\n<p>When you ready to save, use the "OnItemInserting" event:</p>\n\n<pre><code>FormView fv = (FormView)sender;\nDropDownList ddl = (DropDownList)fv.FindControl("ProjectList");\ne.NewValues["ConnectToProject_ID"] = ddl.SelectedValue;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/22503', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2496/']