无法从code加载的DropDownList在FormView的背后? [英] Not possible to load DropDownList on FormView from code behind?

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

问题描述

我有一个用户控件,包含一个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>

我想填充从codebehind DropDownList的。如果这是不包含在一个FormView,我通常只做在Page_Load事件。然而,这并没有一个FormView内工作,因为当我尝试这样做,在访问code DropDownList的,即:

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结合到底层数据源,从而导致一个**'ParentMetric code'具有的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控件的数据绑定()事件进行负载,但随后:

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控件绑定DropDownList的到)

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

推荐答案

好了,找到了一个解决方案:
 1.呼叫从 FormView_ItemCreated 事件DDL填充功能。
 2.修改code填充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.

(任何人都知道为什么我的code格式将所有在这个岗位?搞砸了)

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

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

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