在下拉列表中显示值

时间:2017-01-13 11:34:56

标签: asp.net vb.net dropdown

更新

ddl_category中有一些值,当我选择值时,这些值反对这个“ - ”显示在ddl_item ..所以我想要ddl_item中的选定值,我在ddl_Cateogry中选择...

我在ddl_Cateogry和ddl_item都这样做但没有任何作用

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

2 个答案:

答案 0 :(得分:0)

你的问题是关于winforms而不是你标记的asp.net 如果我正确理解你的问题,你可以使用ddl_Item组合框的TextChanged事件来实现这个结果:

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

答案 1 :(得分:0)

当然,做这样的事情的最好方法是在客户端使用javascript / JSON进行,所以你不必回复。也可以用jquery / AJAX完成,但我的例子将展示如何使用常规的asp.net回发。这是伪代码。

' 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。你需要调整它。但方法就在那里。