views:

1000

answers:

3
+3  Q: 

Formview problem

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)

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

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

Here is the drop down list #2:

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

And here is the MasterProjectDataSource2:

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

Any help on how to get this to work would be greatly appriciated.

A: 

Sounds like the controls aren't being databound properly after the postback.

Are you databinding the first dropdown in the page or in the codebehind? If codebehind, are you doing it in oninit or onload every time?

There might be an issue of the SelectedValue of the second drop down being set to a non-existent item after the postback.

Joel Meador
A: 

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.

Where's Connect_ToProjectId supposed to come from?

Mark Brackett
+2  A: 

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".

(don't know where you get ConnectToProject_ID from)

FormView fv = (FormView)sender;
DropDownList ddl = (DropDownList)fv.FindControl("ProjectList");
ddl.SelectedValue = String.Format("{0:D}", ConnectToProject_ID);

When you ready to save, use the "OnItemInserting" event:

FormView fv = (FormView)sender;
DropDownList ddl = (DropDownList)fv.FindControl("ProjectList");
e.Values["ConnectToProject_ID"] = ddl.SelectedValue;

or "OnItemUpdating"

When you ready to save, use the "OnItemInserting" event:

FormView fv = (FormView)sender;
DropDownList ddl = (DropDownList)fv.FindControl("ProjectList");
e.NewValues["ConnectToProject_ID"] = ddl.SelectedValue;
craigmoliver