首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在下拉列表中显示值

在下拉列表中显示值
EN

Stack Overflow用户
提问于 2017-01-13 19:34:56
回答 2查看 153关注 0票数 0

更新

当我选择value时,ddl_category中有一些值,这些值与ddl_item中显示的"-“相对应。所以我想要我在ddl_Cateogry中选择的ddl_item格式的值...

我在ddl_Cateogry和ddl_item上都这样做了,但都不起作用

代码语言:javascript
运行
复制
Protected Sub ddl_Item_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Item.SelectedIndexChanged
        If (ddl_Item.Text = "-") Then
            ddl_Item.Text = ddl_category.Text
        End If
    End Sub

Protected Sub ddl_cateogry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_cateogry.SelectedIndexChanged
        If (ddl_Item.Text = "-") Then
            ddl_Item.Text =ddl_cateogry.Text
        End If

        GetItem()
        ddl_cateogry.Visible = True
        lbt_ter2.Visible = True
        ddl_Item.Visible = True
        lbt_tie.Visible = True
End Sub
EN

回答 2

Stack Overflow用户

发布于 2017-01-13 21:41:05

你的问题是关于winforms的,而不是你标记的asp.net。

如果我正确理解了您的问题,您可以通过使用ddl_Item combobox的TextChanged事件来实现此结果:

代码语言:javascript
运行
复制
Protected Sub ddl_Item_TextChanged(sender As Object, e As EventArgs) Handles ddl_Item.TextChanged
    If (ddl_Item.Text = "-") Then
        ddl_Item.Text = ddl_subcategory.Text
    End If
End Sub
票数 0
EN

Stack Overflow用户

发布于 2017-01-20 00:38:28

当然,做这件事的最好方法是在客户端用javascript/JSON来做,这样你就不必做回发了。也可以使用jquery/AJAX来实现,但我的示例将展示如何使用常规的asp.net回发来实现。这是伪代码。

代码语言:javascript
运行
复制
' first you need a class that can help you to retrieve the lists for your drop downs
' you need a drop down item class. You will load 3 lists of them
class DropdownItem
    Public Property Group as String
    Public Property Display as String
    Public Property Value as String
end class
' wrap all into one item
class CacheWrapper
    Public Property Categories as List(Of DropdownItem)
    Public Property SubCategories as List(Of DropdownItem)
    Public Property Items as List(Of DropdownItem)
end class
' enum to distinct load
public enum DDLType
    Category = 1
    SubCategory = 2
    Item = 3
end enum

class DropDownLoader

    dim _lock as new Object()
    constant _cacheKey as string = "guid"


    public shared sub LoadDropDown(ddl as DropdownList, group as string, type as DDLType)
         dim wrapper as CacheWrapper = httpcontext.current.cache.get(_cacheKey)
         if wrapper is Nothing then
             SyncLock lockobject  
                 wrapper = httpcontext.current.cache.get(_cacheKey) 
                 if wrapper is Nothing then
                     LoadAndCacheLists()
                     wrapper = httpcontext.current.cache.get(_cacheKey) 
                 end if
             End SyncLock  
         end if

         ' here you actually load drop down based on request
         ddl.Items.clear()
         select case type
             case DDLType.Category
                 wrapper.Categories.forEach(sub(i) ddl.Items.Add(i.display, i.Value))
             case DDLType.SubCategory
                 wrapper.SubCategories.Where(function(i) i.Group = group).ToList().forEach(sub(i) ddl.Items.Add(i.display, i.Value))
             case DDLType.Item
                 wrapper.Items.Where(function(i) i.Group = group).ToList().forEach(sub(i) ddl.Items.Add(i.display, i.Value))
         end select

    end sub

    private shared sub LoadAndCacheLists
        ' here you load from db you Category, subcategory and items and load them into lists.
        ' you set them into CacheWrapper and do something like this using your favorite cache - http, runtime, nCache, enterprise library, etc.
        httpcontext.cache.add(_cacheKey, myCacheWrapper)

        ' for this excercise, I do manual ++!! ONLY to show how structure should look
        dim catList As new List(Of DropdownItem)() ' note - no group here 
        catList.add(new DropdownItem() With { .Display = "Cat 1", .Value = "Cat1Id" })
        catList.add(new DropdownItem() With { .Display = "Cat 2", .Value = "Cat2Id" })

        dim subcatList As new List(Of DropdownItem)() 
        subcatList.add(new DropdownItem() With { .Group = "Cat1Id" .Display = "Sub Cat 1", .Value = "SubCat1Id" })
        catList.add(new DropdownItem() With { .Group = "Cat1Id" .Display = "Sub Cat 2", .Value = "SubCat2Id" })
        catList.add(new DropdownItem() With { .Group = "Cat2Id" .Display = "Sub Cat 3", .Value = "SubCat3Id" })
        catList.add(new DropdownItem() With { .Group = "Cat2Id" .Display = "Sub Cat 4", .Value = "SubCat4Id" })

        dim itemList As new List(Of DropdownItem)() 
        itemList.add(new DropdownItem() With { .Group = "SubCat1Id" .Display = "Item 1", .Value = "Item1Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat1Id" .Display = "Item 2", .Value = "Item2Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat2Id" .Display = "Item 3", .Value = "Item3Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat2Id" .Display = "Item 4", .Value = "Item4Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat4Id" .Display = "Item 5", .Value = "Item5Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat4Id" .Display = "Item 6", .Value = "Item6Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat3Id" .Display = "Item 7", .Value = "Item7Id" })
        itemList.add(new DropdownItem() With { .Group = "SubCat3Id" .Display = "Item 8", .Value = "Item8Id" })
    end sub
end class


' With all above in place, all you have to do is put a little code into your `SelectedIndexChanged` handlers

sub form_load 'page_load?

    ' here you set category
    DropDownLoader.LoadDropdown(ddlCategory, nothing, DDLType.category)
end sub

sub ddlCategory_SelectedIndexChanged

    DropDownLoader.LoadDropdown(ddlSubCategory, ddlCategory.SelectedValue, DDLType.Subcategory)
    ddlItems.Items.Clear()
end sub

sub ddlSubCategory_SelectedIndexChanged

    DropDownLoader.LoadDropdown(ddlItems, ddlSubCategory.SelectedValue, DDLType.Items)

end sub

sub ddlItems_SelectedIndexChanged

    ' here you do your business

end sub

现在,这是通用的方法,你可能需要几个if-then,或者甚至引入一个像_indexChangedbyProgram这样的变量来绕过SelectedIndexChanged中的所有或部分代码。如果您有一个“空项目”,那么您需要逻辑来绕过并清除下一个ddl。您需要对此进行调整。但方法就在那里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41633760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档