How do you use GetProperty.SetValue with the index parameter?

  • Thread starter Thread starter dig_dug_d
  • Start date Start date
D

dig_dug_d

Hello,

I have been struggling with the SetValue method when trying to use
indexed items. I can't figure out what needs to be done to create
the "index As Object()" parameter. Juding from the lack of examples
out there on the net I am sure many people would love to see how this
mysterious parameter works in a real example. Anyone?

Thanks in advance,
Doug
 
Hello,

I have been struggling with the SetValue method when trying to use
indexed items. I can't figure out what needs to be done to create
the "index As Object()" parameter. Juding from the lack of examples
out there on the net I am sure many people would love to see how this
mysterious parameter works in a real example. Anyone?

Thanks in advance,
Doug

Hi Doug,
If I am not mistaken, you must be talking with respect to
setting property values using Reflection ? If it is so, you can use
the following syntax to set a value for the property

ctl.GetType().InvokeMember(strProperty, BindingFlags.SetProperty,
Nothing, ctl, New Object() {strControlResource})

I will explain you all the things in this statement.

ctl - Control object. You can replace this with whatever you want, but
having the GetType() method.
InvokeMember is called to set a named property to some value provided.
strProperty - Name of the property to be set. For example - Text
BindingFlags.SetProperty denotes that we need to set the property.
the last parameter is of special interest now, since it is the actual
value that would be set to the property. Here I have created an Object
array on fly containing the only item strControlResource.

Hope this helps.
If I have misunderstood your question please revert back with relevant
details, so that I may help you.


Thanks,
coolCoder.
 
Hello coolCoder,

Thanks for the reply! I am talking about Reflection.. but I am not
sure your example shows me the piece of the puzzle I am missing. I am
actually working in ASP.net and have a page with tags like so..

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 (Using reflection)? 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.Fin-
dControl(Element), PropValue , Index)

It is that last parameter "index" that I can't seem to find an example
of using. I think it would be used to target one of the ListItem
objects. In your example what exactly is "strControlResource"? I have
used reflection to set object properties with no problem.. it is the
index aspect that I can't figure out what needs to be passed in.

Thanks for your help!
Doug
 
Hello coolCoder,

Thanks for the reply! I am talking about Reflection.. but I am not
sure your example shows me the piece of the puzzle I am missing. I am
actually working in ASP.net and have a page with tags like so..

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 (Using reflection)? 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.Fin-
dControl(Element), PropValue , Index)

It is that last parameter "index" that I can't seem to find an example
of using. I think it would be used to target one of the ListItem
objects. In your example what exactly is "strControlResource"? I have
used reflection to set object properties with no problem.. it is the
index aspect that I can't figure out what needs to be passed in.

Thanks for your help!
Doug

Hi Doug,

I dont know why you are trying to use Reflection in this case.
It is simple to select items in drop down using FindByText and
FindById methods.
These methods return ListItem object, and if you set the Selected
property of this object as true then it will appear to be selected.

Caution : While using drop downs, please make sure to call
ClearSelection() method before programatically changing the selected
item; otherwise it throws exception
when you select some other item, when already there is a selected item
in the collection.

Can you tell me why you are trying to use Reflection for this ? Any
special reason ?

Thanks,
coolCoder
 
Hello coolCoder,

Thanks for the reply! I am talking about Reflection.. but I am not
sure your example shows me the piece of the puzzle I am missing. I am
actually working in ASP.net and have a page with tags like so..

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 (Using reflection)? 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.Fin-
dControl(Element), PropValue , Index)

It is that last parameter "index" that I can't seem to find an example
of using. I think it would be used to target one of the ListItem
objects. In your example what exactly is "strControlResource"? I have
used reflection to set object properties with no problem.. it is the
index aspect that I can't figure out what needs to be passed in.

Thanks for your help!
Doug

Hi Doug,

Sorry I forgot to answer your question in the last post.

strControlResource - in my case, was a string value which I wanted to
set to the property with strProperty as its name.

About the "index" thing, I don't have much experience setting the
Indexed properties.

Hope I am clear enough.

Thanks,
coolCoder
 
dig_dug_d said:
<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 (Using reflection)?

You don't need Reflection for this.
Indeed, you don't need Reflection for /most/ things.

In this case, you /know/ there's going to be a ListBox coming at you and
you /know/ it's going to some ListItems, the second one of which you
want to select:

Dim lb As ListBox _
= DirectCast( Me.FindControl( "ListBox1" ), ListBox )

If lb IsNot Nothing _
AndAlso lb.ListItems.Count > 2 _
Then
lb.ListItems(1).Selected = True
End If

HTH,
Phill W.
 
Thanks for the replies,

I do need to use reflection.. as my controls are being created
dynamically. The use of the listitem was just an example of where the
SetValue index parameter might be used.. my question is really about
that aspect of the SetValue method. It seems to be a rarely used in
examples.. I would say never used.. but I am sure there is an example
out there that I have not found yet. The "index As Object()" is the
confusing part.. Any ideas?????

Thanks for the help!
Doug
 
dig_dug_d said:
I do need to use reflection.. as my controls are being created
dynamically.

You can assign an .ID to them when they are created, then use that ID to
figure out which control sent an event.

Andrew
 
Thanks for the reply,

I'm sorry I got this off on a tangent..the example I gave was only to
frame the question "how do use the index parameter of the SetValue
method". I appreciate the help.. but there is this crazy parameter
that no one seems to know how to use and it seems others have asked..
but I have yet to find a good example on how this aspect of the method
works. I really do appreciate all the help and time people spend
replying... and if I ever do find the answer I will most certainly
post it.

Thanks,
Doug
 
dig_dug_d said:
Thanks for the reply,

I'm sorry I got this off on a tangent..the example I gave was only
to frame the question "how do use the index parameter of the
SetValue method". I appreciate the help.. but there is this crazy
parameter that no one seems to know how to use and it seems others
have asked.. but I have yet to find a good example on how this
aspect of the method works. I really do appreciate all the help and
time people spend replying... and if I ever do find the answer I
will most certainly post it.


I don't find a Selected property with the Listbox control, neither Winforms,
nor Webforms.

I fail to see the problem. Indexed properties are properties with arguments,
for example

o.p(17) = 12

or

o.p(17, 24) = 12


So:

Private Class C
Public Property P(_
ByVal arg1 As Integer, ByVal arg2 As Integer) As Integer
Get

End Get
Set(ByVal value As Integer)
MsgBox(arg1 & " " & arg2)
End Set
End Property
End Class

Usage:
Dim o As New C
Dim Args(1) As Object

Args(0) = 17
Args(1) = 24

o.GetType.GetProperty("P").SetValue(o, 17, Args)



Armin
 
dig_dug_d said:
I do need to use reflection as my controls are being created
dynamically.

No you don't.

/If/ you know the name of a control then you can use FindControl to find
it and manipulate it.

Even if you /don't/ know the name of the control you want (and I'm
slightly puzzled as to why you wouldn't) you can iterate through the
page's Controls collection and recursively down through each "container"
control to find a control that you're interested in.

Having found one, you can /ask/ that Control what Type it is ...

If TypeOf ctl Is ListBox Then
. . .

.... and, if it's one you want, you can cast it to the correct Type and
manipulate it as required.

/None/ of the above requires Reflection.

IMHO, you must know /something/ about these controls; how can you work
with them otherwise? It's like blindfolding a Vet and asking him to
perform surgery on an unknown animal.

HTH,
Phill W.
 
Back
Top