我的表单上有2个DropDownList
控件,第二个控件使用第一个SelectedValue
作为其绑定参数之一。
两个DropDownList
控件都在FormView.InsertItemTemplate
中,SelectedValue
属性使用绑定表达式绑定到FormView
的数据源。
第一次FormView
在插入模式下呈现时,一切正常。问题是在第一个AutoPostBack
的{{1}}之后,DropDownList
没有(重新)绑定,但是自第二个FormView
ControlParameter
以来已更改,它已绑定(按预期),但在第二个DDL的绑定表达式上发生异常,我假设,因为DropDownList
没有绑定该传递:
System.InvalidOperationException:数据绑定方法,如Eval(), XPath()和Bind()只能在数据绑定的上下文中使用 控制。
这是标记:
FormView
我的问题是:使这项功能有效的最佳方法是什么?是否可以将两个DDL保留在模板中?我宁愿避免使用AJAX工具包或其他客户端解决方案。
答案 0 :(得分:8)
当我们在像DetailsView/FormView
这样的数据绑定控件中使用级联下拉列表时,这是一个问题,我已经多次面对它了。您必须从第二个下拉列表SelectedValue='<%# Bind("AttorneyID") %>'
中删除绑定表达式,然后它才能正常工作。
其次,如果删除Binding表达式,则必须在FormView ItemInserting
事件中手动传递值。 e.g。
protected void frmAsset_ItemInserting(object sender, FormViewInsertEventArgs e)
{
eValues["AttorneyID"] = ((DropDownList)((FormView)sender).FindControl("ddlAttorny")).SelectedValue;
}
答案 1 :(得分:4)
实际上我发布这个答案,万一其他人像我一样被卡住了。 穆罕默德·阿赫塔尔所说的完美无缺,但我找到了一个更简单的解决方案 在
<asp:DropDownList ID="ddlAttorney" runat="server" DataSourceID="dsAttorney" DataValueField="AttorneyID" DataTextField="AttorneyNameWithCount" SelectedValue='<%# Bind("AttorneyID") %>'>
将Bind("AttorneyID")
更改为DataBinder.Eval (Container.DataItem, "AttorneyID")
它完美无缺!
编辑:我的示例代码:
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContent" runat="Server">
<asp:DetailsView ID="dv" runat="server" Height="50px" DataSourceID="ODS" DefaultMode="Insert"
AutoGenerateRows="False" OnItemCommand="dv_ItemCommand" OnItemInserted="dv_ItemInserted"
DataKeyNames="Id" OnItemUpdated="dv_ItemUpdated" CssClass="DetailsView"
>
<Fields>
<asp:TemplateField HeaderText="Page Name:">
<ItemTemplate>
<asp:Label ID="txtPageName" runat="server" Text="<%#Bind('PageName') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPageName" runat="server" Text="<%#Bind('PageName') %>" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent:">
<ItemTemplate>
<asp:Label ID="txtParentPageName" runat="server" Text='<%#Bind("ParentPageName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="lstParentPage" DataSourceID="ParentPageODS"
AppendDataBoundItems="true" DataTextField="PageName" DataValueField="Id" SelectedValue="<%#Bind('ParentPage') %>"
AutoPostBack="True">
<asp:ListItem Text="-Root-" Value="" />
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="After...">
<ItemTemplate>
<asp:Label ID="txtPreviousPage" runat="server" Text='<%#Bind("PageOrder") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="lstPageOrder" AppendDataBoundItems="true" DataTextField="PageName" DataSourceID="PageOrderODS" DataValueField="PageOrder" EnableViewState="False" SelectedValue='<%# DataBinder.Eval (Container.DataItem, "PageOrder") %>'>
<asp:ListItem Text="-First-" Value="" />
</asp:DropDownList>
<asp:ObjectDataSource ID="PageOrderODS" runat="server" SelectMethod="SelectByParent"
TypeName="SirM2X.Pages">
<SelectParameters>
<asp:ControlParameter ControlID="lstParentPage" Name="ParentPage" PropertyName="SelectedValue" />
</SelectParameters>
</asp:ObjectDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dummy Page?">
<ItemTemplate>
<asp:Label runat="server" ID="txtDummyPage" Text="<%#Bind('IsDummyText') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkIsDummy" runat="server" Checked="<%#Bind('IsDummy') %>" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="<%$Resources:Resources, Update %>" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="<%$Resources:Resources, Cancel %>" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Button ID="btnInsert" runat="server" CausesValidation="True" CommandName="Insert"
Text="<%$Resources:Resources, Insert %>" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="<%$Resources:Resources, Cancel %>" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="<%$Resources:Resources, Edit %>" />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ODS" runat="server" DeleteMethod="DeleteRow" InsertMethod="InsertRow"
SelectMethod="SelectRow" TypeName="SirM2X.Pages" UpdateMethod="UpdateRow" OnInserting="ODS_Inserting"
OnUpdating="ODS_Updating">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="PageName" Type="String" />
<asp:Parameter Name="CreatedBy" Type="String" />
<asp:Parameter Name="ParentPage" Type="Int32" />
<asp:Parameter Name="PageOrder" Type="Int32" />
<asp:Parameter Name="IsDummy" Type="Boolean" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="ID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="PageName" Type="String" />
<asp:Parameter Name="ParentPage" Type="Int32" />
<asp:Parameter Name="PageOrder" Type="Int32" />
<asp:Parameter Name="IsDummy" Type="Boolean" />
<asp:Parameter Name="DeleteState" Type="Boolean" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ParentPageODS" runat="server" SelectMethod="SelectAll"
TypeName="SirM2X.Pages"></asp:ObjectDataSource>
答案 2 :(得分:2)
这可能会有点晚,但迟到总比没有好:
Protected Sub DetailsView1_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
e.NewValues("AtendeeSubType") = DirectCast(DirectCast(sender, DetailsView).FindControl("dropdownlist3"), DropDownList).SelectedValue
End Sub
我为详细视图的事件ItemUpdating测试了这个,但我认为它适用于表单视图,只需切换所需的部分即可。
编辑:您可以查看此参考: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemupdating.aspx
答案 3 :(得分:1)
我是这样做的......
<asp:SqlDataSource ID="sqldsDDPlant" runat="server" ConnectionString="<%$ ConnectionStrings:SeedTrackerConnectionString %>"
SelectCommand="SELECT * FROM [Plant] ORDER BY [Plant]"></asp:SqlDataSource>
<asp:SqlDataSource ID="sqldsDDType" runat="server" ConnectionString="<%$ ConnectionStrings:SeedTrackerConnectionString %>"
SelectCommand="SELECT * FROM [Type] ORDER BY [Type]" FilterExpression="PLID = '{0}'">
<FilterParameters>
<asp:ControlParameter Name="plantParam" ControlID="DVSeedTracker$ddPlant" PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TemplateField HeaderText="Plant">
<InsertItemTemplate>
<asp:DropDownList ID="ddPlant" runat="server" AutoPostBack="true" SelectedValue='<%# Bind("PLID") %>'
DataSourceID="sqldsDDPlant" DataTextField="Plant" DataValueField="PLID" AppendDataBoundItems="True">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator
id="RequiredFieldValidator2"
runat="server"
ControlToValidate="ddPlant"
Display="Static"
ErrorMessage="*Required" CssClass="RequiredField" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<InsertItemTemplate>
<asp:DropDownList ID="ddType" runat="server"
DataSourceID="sqldsDDType" DataTextField="Type" DataValueField="TYPID" AppendDataBoundItems="False">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator
id="RequiredFieldValidator3"
runat="server"
ControlToValidate="ddType"
Display="Static"
ErrorMessage="*Required" CssClass="RequiredField" />
</InsertItemTemplate>
</asp:TemplateField>