Today a friend of mine had a situation where his DropDownLists' data had entries that required duplicate values but different text. When the SelectedIndexChanged event was fired it would always return the item with the lowest index of all the duplicate items that matched the selected one. As a point of conversation we both conjectured that the internal handling of the method had to be searching for the proper entry by Value and not the actual index. Well, I went home and sat with Reflector for a little while and here's what I've found. I'm going to put some very basic explanations in the code for a few parts just to make myself recite a bit of what I'm learning right now:
CIL class definition of the DropDownList control:
.class public auto ansi beforefieldinit DropDownList
extends System.Web.UI.WebControls.ListControl
implements System.Web.UI.IPostBackDataHandler
{
[snip….]
Within this class we find a definition for the LoadPostData which would be the place that handles the postback:
.method family hidebysig newslot virtual instance bool LoadPostData(string postDataKey, [System]System.Collections.Specialized.NameValueCollection postCollection) cil managed
{
}
---------------
If we look at LoadPostData we'll find
.method family hidebysig newslot virtual instance bool LoadPostData(string postDataKey, [System]System.Collections.Specialized.NameValueCollection postCollection) cil managed
{
.maxstack 4
.locals init ( - this defines two local variables that will be used; textArray1 and num1
[0] string[] textArray1,
[1] int32 num1)
L_0000: ldarg.2
L_0001: ldarg.1
L_0002: callvirt instance string[]
ZSystem]System.Collections.Specialized.NameValueCollection::GetValues(string) – this method gets the values from postCollection that was passed in through the method call.
L_0007: stloc.0 – stloc.0 stores the result of the previous call in textArray1
L_0008: ldarg.0 – we now load postDataKey onto the stack (*)
L_0009: callvirt instance void System.Web.UI.WebControls.BaseDataBoundControl::EnsureDataBound()– checking to see if it’s a databound contol..
L_000e: ldloc.0– load textArray1 back onto the stack
L_000f: brfalse.s L_003d – “branch false” – if the result from the EnsureDataBound call was zero we go to the L_003d address which looks like this “L_003d: ldc.i4.0” further down the code block (seems like this instruction should have come before the one previously… oh well, I need to learn some more so I can understand this)
L_0011: ldarg.0 – here we’re loading the postDataKey onto the stack
L_0012: ldarg.1 – here we’re loading the postCollection onto the stack
L_0013: ldloc.0 – here we’re loading textArray1 onto the stack.
L_0014: ldc.i4.0
L_0015: ldelem.ref
L_0016: call instance void System.Web.UI.Control::ValidateEvent(string, string)
L_001b: ldarg.0
L_001c: callvirt instance System.Web.UI.WebControls.ListItemCollection System.Web.UI.WebControls.ListControl::get_Items()
L_0021: ldloc.0
L_0022: ldc.i4.0
L_0023: ldelem.ref
L_0024: ldc.i4.0
L_0025: callvirt instance int32 System.Web.UI.WebControls.ListItemCollection::FindByValueInternal(string, bool) – here is where we break out to do the actual ‘finding’ of the item that was selected… see the next code block…
L_002a: stloc.1
L_002b: ldarg.0
L_002c: callvirt instance int32 System.Web.UI.WebControls.ListControl::get_SelectedIndex()
L_0031: ldloc.1
L_0032: beq.s L_003d
L_0034: ldarg.0
L_0035: ldloc.1
L_0036: call instance void System.Web.UI.WebControls.ListControl::SetPostDataSelection(int32)
L_003b: ldc.i4.1
L_003c: ret
L_003d: ldc.i4.0
L_003e: ret
}
In this method we see the values being compared to each other (get_Value and Equals())
.method assembly hidebysig instance int32 FindByValueInternal(string value, bool includeDisabled) cil managed
{
.maxstack 2
.locals init (
[0] int32 num1,
[1] System.Web.UI.WebControls.ListItem item1,
[2] int32 num2,
[3] [mscorlib]System.Collections.IEnumerator enumerator1,
[4] [mscorlib]System.IDisposable disposable1)
L_0000: ldc.i4.0
L_0001: stloc.0
L_0002: ldarg.0
L_0003: ldfld [mscorlib]System.Collections.ArrayList System.Web.UI.WebControls.ListItemCollection::listItems
L_0008: callvirt instance [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.ArrayList::GetEnumerator()
L_000d: stloc.3
L_000e: br.s L_003d
L_0010: ldloc.3
L_0011: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()
L_0016: castclass System.Web.UI.WebControls.ListItem
L_001b: stloc.1
L_001c: ldloc.1
L_001d: callvirt instance string System.Web.UI.WebControls.ListItem::get_Value()
L_0022: ldarg.1
L_0023: callvirt instance bool string::Equals(string)
L_0028: brfalse.s L_0039
[…snip…]
}
..and the get_Value method:
.method public hidebysig specialname instance string get_Value() cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: ldfld string System.Web.UI.WebControls.ListItem::value
L_0006: brfalse.s L_000f
L_0008: ldarg.0
L_0009: ldfld string System.Web.UI.WebControls.ListItem::value
L_000e: ret
L_000f: ldarg.0
L_0010: ldfld string System.Web.UI.WebControls.ListItem::text
L_0015: brfalse.s L_001e
L_0017: ldarg.0
L_0018: ldfld string System.Web.UI.WebControls.ListItem::text
L_001d: ret
L_001e: ldsfld string [mscorlib]System.String::Empty
L_0023: ret
}
-------------------What does this mean?----------------
So, it looks like it does and value comparison *and* and text comparison in order to find the correct item. I wonder why…. I might try to change it to base it on the actual index and see what happens.

0 Comments:
Post a Comment
<< Home