Use Existing Object by String Name

  • Thread starter Damon Gravning via DotNetMonster.com
  • Start date
D

Damon Gravning via DotNetMonster.com

I am trying to use one function to handle a SelectedIndexChanged event for 5 radio button groups that correspond to 5 populated Datagrids. The radio buttons have a choice of 5,10 25 for a datagrid.pagesize.

I am having trouble trying to find a way to pass the name of the existing datagrid object to the function and to have it assign its the appropriate datasource, pagesize and rebind.

I found a post with this ControlFromName Function and have tried to use it but it fails with s SystemNullException in the function. Please bear with my lack of knowledge on this, reflection and some of this heavy programming is new to me. Here i my code blurb and the function as I have it. I did change it to use WebControl instead of Control since this is on as aspx page.

Thanks for any help. Damon


Dim dv As DataView
Dim dg As DataGrid
dg = CType(ControlFromName("dgToH1"), DataGrid)
dg.DataSource = dv
dg.DataBind()

Private Function ControlFromName(ByVal name As String) As WebControl
Dim o As Object
o = Me.GetType().GetField(name, Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase).GetValue(Me)

Return (CType(o, WebControl))
End Function
 
Z

Zoury

hi!

i've just answered this one on the french ng.. the answer is still in my
clipboard :O)
http://groups.google.com/groups?threadm=#[email protected]

--
Best Regards
Yanick
"Damon Gravning via DotNetMonster.com" <[email protected]> a écrit
dans le message de
I am trying to use one function to handle a SelectedIndexChanged event for
5 radio button groups that correspond to 5 populated Datagrids. The radio
buttons have a choice of 5,10 25 for a datagrid.pagesize.
I am having trouble trying to find a way to pass the name of the existing
datagrid object to the function and to have it assign its the appropriate
datasource, pagesize and rebind.
I found a post with this ControlFromName Function and have tried to use it
but it fails with s SystemNullException in the function. Please bear with
my lack of knowledge on this, reflection and some of this heavy programming
is new to me. Here i my code blurb and the function as I have it. I did
change it to use WebControl instead of Control since this is on as aspx
page.
 
G

Guest

Could you just pass the control as an object to the Function;

Function myfunction(mygrid as DataGrid)
mygrid.property...
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top