How to set ListItem properties using Reflection (VB.net).

How to set ListItem properties using Reflection (VB.net).

Post by dig_dug_ » Thu, 22 Nov 2007 16:18:32



Hello,

I have been struggling with the SetValue method when trying to use
indexed items. I can not figure out what needs to be done to create
the "index As Object()" parameter.

Lets say I have this in my page..

<asp:ListBox ID="ListBox1" runat="server" EnableViewState="False"
SelectionMode="Multiple">
<asp:ListItem Value="L1">List1</asp:ListItem>
<asp:ListItem Value="L2">List2</asp:ListItem>
<asp:ListItem Value="L3">List3</asp:ListItem>
</asp:ListBox>

And in the code behind I want to set the second item's selected
property to "True"... how would this be done? I'm currently working
with something like this..

Dim Element As String = "ListBox1"
Dim PropName As String = "Selected"
Dim PropValue As String = "True"
Dim Index = New Object()

Page.FindControl(Element).GetType().GetProperty(PropName).SetValue(Page.FindControl(Element),
PropValue , Index)


If I am way off and using the index parameter isn't needed.. I am sure
many people would love to see how this mysterious parameter works in a
real example.

Thanks in advance!
Doug