Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在下拉列表中显示值_Asp.net_Vb.net_Dropdown - Fatal编程技术网

Asp.net 在下拉列表中显示值

Asp.net 在下拉列表中显示值,asp.net,vb.net,dropdown,Asp.net,Vb.net,Dropdown,更新 当我选择值时,ddl_类别中有一些值。这些值与ddl_项目中显示的“-”相对应。。所以我想要在ddl_目录中选择的ddl_项中选择的值 我在ddl_目录和ddl_项上都这样做,但没有任何效果 Protected Sub ddl_Item_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Item.SelectedIndexChanged If (ddl_Item.Text = "-") Th

更新

当我选择值时,ddl_类别中有一些值。这些值与ddl_项目中显示的“-”相对应。。所以我想要在ddl_目录中选择的ddl_项中选择的值

我在ddl_目录和ddl_项上都这样做,但没有任何效果

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

您的问题是关于winforms,而不是您标记的asp.net。
如果我正确理解了您的问题,您可以通过使用ddl_项组合框的
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

当然,这样做的最好方法是在客户机上使用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,或者甚至引入一个变量,如
\u indexChangedbyProgram
,以绕过
SelectedIndexChanged
中的所有或部分代码。若您有一个“空项”,那个么您需要绕过并清除下一个ddl的逻辑。你需要调整这个。但是方法是存在的。

您尝试了什么?可以用部分代码编辑您的帖子吗?@AfnanAhmad check update您需要提供一些示例值和/或更好地解释。我个人在…如果我从子目录中选择abc值…你需要更多信息吗@我做这个。。。但价值不可见。。我的意思是,如果我从子类别中选择了值,假设我从子类别中选择了水果,那么如果针对项目中的水果值显示“-”,那么我想从项目中的子类别中获取水果值,而不是“-”您是否在ddl_项目组合框中定义了textchanged事件?很抱歉,我没有得到您??简单地说,您是否将
受保护的子ddl\u子类别\u SelectedIndexChanged
替换为
受保护的子ddl\u项目\u TextChanged
。但是乳清替换,而我想在ddl_子类别上执行此操作。。当从子类别中选择任何值时,ddl_项目值中的这些值将显示为SO com;licateddddd@cooluser不是真的。这只是解决你手头问题的常规模式。引用:“将复杂性引入系统的原因是为了实现扩展的简单性。”或者,您可以用维护来代替扩展。如果明天,比如说,您需要引入第四个下拉列表,您的系统可以轻松支持它。