Formview Dropdown 2 Depends on Dropdown 1

I have a form view, in the editing template I have two descents. Drop-down list 1 explicitly sets a list of valid values. It is also set to autorun. Drop 2 is a data binding to a data source object, this source object uses the first drop-down menu as one of its parameters. (The idea is that falling down 1 limits what is shown in drop-down list 2)

In the first view of the editing template for an element, it works fine. But if drop 1 has another element selected after it and generates an error

Data binding methods such as Eval (), XPath (), and Bind () can only be used in the context of database management.

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 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 make this work would be greatly appreciated.

+4
source share
3 answers

I had a similar problem with related dropdownlists in FormView. I worked around it, setting the selected value manually in the form of the "OnDataBound" form.

(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 are 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 are ready to save, use the "OnItemInserting" event:

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

It seems that after the postback, the controls are not correctly bound.

Do you bind the first drop-down list on a page or in code? If codebehind, do you do this in on_init or on_load every time?

Perhaps the problem is after the SelectedValue of the second drop-down list was set to a nonexistent item after the postback.

0
source

If your second drop-down is in data management mode (like a repeater), I'm not sure what you're trying to bind SelectedValue to. Apparently, none of them is .NET, since it is probably where the error occurs.

Where is Connect_ToProjectId supposed to be?

0
source

Source: https://habr.com/ru/post/1276407/


All Articles