【发布时间】:2010-03-12 19:19:53
【问题描述】:
我有一个 UserControl,包含一个 FormView,包含一个 DropDownList。 FormView 绑定到数据控件。
像这样:
<asp:FormView ID="frmEdit" DataKeyNames="MetricCode" runat="server" DefaultMode="Edit" DataSourceID="llbDataSource" Cellpadding="0" >
<EditItemTemplate>
<asp:DropDownList ID="ParentMetricCode" runat="server" SelectedValue='<%# Bind("ParentMetricCode") %>' />
</EditItemTemplate>
<asp:FormView>
我正在尝试从代码隐藏中填充 DropDownList。如果这不包含在 FormView 中,我通常会在 Page_Load 事件中执行此操作。但是,这在 FormView 中不起作用,只要我尝试这样做,访问代码中的下拉列表,即:
theListcontrol = CType(formView.FindControl(listControlName), ListControl)
...调用了FormView的数据绑定机制,当然会尝试将DropDownList绑定到底层数据源,导致**'ParentMetricCode'有一个无效的SelectedValue,因为它不存在项目列表。 “参数名称:值 ...”错误,因为 DropDownList 尚未填充。
我尝试在 FormView 的 DataBinding() 事件中执行加载,但是:
theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)
...失败,因为此时 FormView.Controls.Count = 0。
这不可能吗? (我不想使用辅助 ObjectDataSource 将下拉列表绑定到)
【问题讨论】:
-
您究竟为什么要尝试从代码隐藏中填充下拉列表?你能把它绑定到一个单独的数据源吗?
-
我不需要,但我想。我不喜欢数据源控件,我将一个用于主数据源的唯一一个是这样我可以使用双向数据绑定......但我发现它不值得。
标签: asp.net .net drop-down-menu formview