无法从后面的代码加载 FormView 上的 DropDownList? [英] Not possible to load DropDownList on FormView from code behind?

查看:16
本文介绍了无法从后面的代码加载 FormView 上的 DropDownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl,包含一个 FormView,包含一个 DropDownList.FormView 绑定到数据控件.

I have a UserControl, containing a FormView, containing a DropDownList. The FormView is bound to a data control.

像这样:

<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 中不起作用,只要我尝试这样做,访问代码中的下拉列表,即:

I am trying to populate the DropDownList from the codebehind. If this was not contained in a FormView, I would normally just do it in the Page_Load event. However, that does not work within a FormView, as as soon as I try to do it, accessing the dropdownlist in code, i.e.:

theListcontrol = CType(formView.FindControl(listControlName), ListControl)  

...调用FormView的数据绑定机制,当然会尝试将DropDownList绑定到底层数据源,导致**'ParentMetricCode'有一个无效的SelectedValue,因为它不存在于项目列表.参数名称:值..."错误,因为尚未填充 DropDownList.

...the data binding mechanism of the FormView is invoked, which, of course, tries to bind the DropDownList to the underlying datasource, causing a **'ParentMetricCode' has a SelectedValue which is invalid because it does not exist in the list of items. "Parameter name: value ..." error, since the DropDownList has not yet been populated.

我尝试在 FormView 的 DataBinding() 事件中执行加载,但是:

I tried performing the load in the DataBinding() event of the FormView, but then:

theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)

...失败,因为此时 FormView.Controls.Count = 0.

...fails, as the FormView.Controls.Count = 0 at that point.

这不可能吗?(我不想使用辅助 ObjectDataSource 将下拉列表绑定到)

Is this impossible? (I do not want to have to use a secondary ObjectDataSource to bind the dropdownlist to)

推荐答案

好的,找到一个"解决方案:
1. 从 FormView_ItemCreated 事件调用 DDL 填充函数.
2. 修改代码以填充 DDL,如下所示:

Ok, found "a" solution:
1. Call the DDL populate function from the FormView_ItemCreated event.
2. Modify the code to populate the DDL like so:

Public Overloads Shared Sub BindListControl(Of theLLBLgenEntityType As EntityBase) _   
        (ByVal formView As FormView, _
        ByVal listControlName As String, _
        ByVal theCollection As CollectionCore(Of theLLBLgenEntityType), _
        ByVal DataValueField As EntityField, _
        ByVal DataTextField As EntityField, _
        ByVal IncludeEmptyItem As Boolean)

    If formView.CurrentMode = FormViewMode.Edit Then
        Dim theListcontrol As System.Web.UI.WebControls.ListControl
        theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)

    For Each entity As theLLBLgenEntityType In theCollection
        theListcontrol.Items.Add(New ListItem(entity.Fields(DataTextField.Name).CurrentValue.ToString, entity.Fields(DataValueField.Name).CurrentValue.ToString))
    Next

    If IncludeEmptyItem Then 
        theListcontrol.Items.Insert(0, New ListItem("", ""))
    End If
End Sub

这有点似乎基本上归结为,您不能在运行时以编程方式在 FormView 中进行额外的数据绑定".

It kinda seems to basically boil down to, you cannot do additional "Databinding", programatically at runtime, within a FormView.

(有谁知道为什么我的代码格式在这篇文章中全被搞砸了??)

(Anyone know why the formatting of my code is all screwed up in this post??)

此解决方案引发了另一批与插入新项目相关的问题.经过一番折腾,我终于找到了解决办法.在这一点上,我基本上是建议要么完全避免 FormView 控件,要么绝对避免以编程方式更改绑定控件.希望为每个 DropDownList 使用单独的数据控件可能会更好,但这也可能是一厢情愿的想法.

This solution raises yet another batch of issues, related to inserting new items. After a lot of screwing around I eventually found a way around that. At this point, I am basically at the point where I would recommend either avoiding the FormView control entirely, or definitely avoid programatically altering bound controls. Hopefully, perhaps using seperate data controls for each DropDownList might work better, but this might be wishful thinking as well.

这篇关于无法从后面的代码加载 FormView 上的 DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆