FinByValue 无法在 UserControl DropDownList 的上下文中工作

发布于 2024-10-25 12:13:25 字数 1656 浏览 2 评论 0原文

我正在用这个拔我的头发。

我有一个 Employee 下拉列表用户控件 (uxEmployee),并且我已将 SelectedValue 方法公开为属性 (SelectedValue)。

在我的页面中,我尝试根据我的数据设置值,或者将值添加到列表中(如果当前不存在)。

无论我做什么,都是将设置值添加到列表中。即使该值已经在列表中;它仍在增加价值。基本上,无论我做什么,FindByValue 总是返回“Nothing”。任何帮助将不胜感激。

    Public Property SelectedValue() As String

    Get
        Return uxEmployee.SelectedValue
    End Get

    Set(ByVal value As String)

        If Not uxEmployee.Items.FindByValue(value) Is Nothing Then
            uxEmployee.SelectedValue = value
        Else
            uxEmployee.Items.Insert(0, New ListItem(value, value))
            uxEmployee.AppendDataBoundItems = True
            uxEmployee.DataBind()
        End If

    End Set
End Property

Public WriteOnly Property Department() As String
    Set(ByVal value As String)
        _department = value
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not (IsPostBack) Then
        Dim emp As New Employee()

        With uxEmployee
            If _department <> String.Empty Then
                .DataSource = emp.GetByDept(_department)
            Else
                .DataSource = emp.GetList()
            End If
            .DataTextField = "FullName"
            .DataValueField = "UserName"
            .DataBind()
        End With
    End If
End Sub

我的调用页面在页面加载中使用以下内容.. uxSalesleadv 是 uxemployee 的实例

    Dim objPrj As ServiceProject = New ServiceProject()
        objPrj = objPrj.GetItem(prjID)
        With objPrj
            uxSalesLead.SelectedValue = .SalesLead
            End With

I am pulling my hair out with this one.

I have an Employee dropdownlist usercontrol (uxEmployee) and I have exposed the SelectedValue method as a property(SelectedValue).

Within my page I am trying to either set the value based on my data or add the value to the list if it is not currently on there.

No matter what I do it is adding the Set value to the list. Even when the value is already on the list; it is still adding the value. Basically FindByValue is always returning "Nothing" no matter what I do. Any help would be appreciated.

    Public Property SelectedValue() As String

    Get
        Return uxEmployee.SelectedValue
    End Get

    Set(ByVal value As String)

        If Not uxEmployee.Items.FindByValue(value) Is Nothing Then
            uxEmployee.SelectedValue = value
        Else
            uxEmployee.Items.Insert(0, New ListItem(value, value))
            uxEmployee.AppendDataBoundItems = True
            uxEmployee.DataBind()
        End If

    End Set
End Property

Public WriteOnly Property Department() As String
    Set(ByVal value As String)
        _department = value
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not (IsPostBack) Then
        Dim emp As New Employee()

        With uxEmployee
            If _department <> String.Empty Then
                .DataSource = emp.GetByDept(_department)
            Else
                .DataSource = emp.GetList()
            End If
            .DataTextField = "FullName"
            .DataValueField = "UserName"
            .DataBind()
        End With
    End If
End Sub

my calling page uses the following in page load.. uxSalesleadv is an instance of uxemployee

    Dim objPrj As ServiceProject = New ServiceProject()
        objPrj = objPrj.GetItem(prjID)
        With objPrj
            uxSalesLead.SelectedValue = .SalesLead
            End With

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-11-01 12:13:25

该方法执行精确匹配,并且区分大小写且不区分区域性。您是否检查过调试器中的值是否相同?

The method performs an exact match and is case-sensitive and culture-insensitive. Have you checked that the values are identical in the debugger?

抱着落日 2024-11-01 12:13:25

尝试使用FindByName()。如果有效,则表明在您最初设置数据源时未正确设置值。

Try using FindByName(). If it works, then the values are not being set properly when you initially set the data source.

信愁 2024-11-01 12:13:25

经过多次尝试、错误和调试,我已经找出了问题所在。

当我将数据分配从 page_load 移至 pre_render 事件时,我能够使控件正常工作。但这并不理想。我最终发现用户控件在实际数据绑定发生之前设置了值。

我现在已将逻辑移出 Set 属性,并将逻辑添加到控件 page_load 数据绑定中,以检查先前的 SET 值是否在数据绑定列表中。如果不是,则会添加该项目。这将使我能够在整个应用程序中使用用户控件,而无需任何额外的页面代码,也无需担心项目不在列表中的错误。

After much trial and error and debugging I have figured out the problem.

I was able to get the control to work correctly when i moved my data assignment from the page_load to the pre_render event. This was not ideal though.. I eventually found that the usercontrol was setting the value before the actual databinding was taking place.

I have now moved the logic out of my Set property and now added logic to my controls page_load databinding to check if the previous SET value is in the databind list. If it isnt then it adds the item. This will allow me to use the usercontrol throughout my apps without any additional page code and worrying about item not in list errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文