を、このような何かを行うための最善の方法は、上の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
、これは一般的なアプローチで、あなたがあれば、その後、いくつかの必要がある、あるいはSelectedIndexChanged
に全部または一部のコードをバイパスする_indexChangedbyProgram
のような変数を導入することができます。 「空のアイテム」がある場合、バイパスして次のddlをクリアするロジックが必要です。あなたはこれを調整する必要があります。しかし、アプローチはそこにあります。
あなたは何を試しましたか? –
あなたのコードの一部で投稿を編集できますか? –
@AfnanAhmadチェックアップデート –