Getting the Control ID programatically

V

vsr

How can we get the control ID programatically?Scenario is i have one Web USer
Control and it can be used any page with different IDs, i am trying to get
the Control ID programatically in one of the Methods i have inside this Web
User Control.
 
W

Willy Van den Driessche

maybe I'm missing something but how about just using "Me.ID" (in VB.NET).

ID is a property of System.Web.UI.Control and therefore available for all
controls.
 
V

vsr

Yes , you are rite we can use it , i am sorry actually i didnt put here
actual requirement, the requirement is i want to know the calling method of
the parent page...
I have User Control Say "WebUC" and using this in page "SamplePage.aspx"
and this page has the method getData() and in this method i am calling one
of the public methods of the User Control.And my requirement is i want to
know the Calling Method of the Page( .. in this case its "getData()" ) in the
User Control's Method.

I am for not putting the complete scenario before.
 
B

Brock Allen

You want Control.ClientID if you intend to use it from javascript in the
rendered page. If you want the scoped ID on the server, check out Control.UniqueID.
 
W

Willy Van den Driessche

The simplest way would be to have some variable to keep this information in
the page.

The generic way could use reflection to get at that information (but that
might be shooting a bug with a cannon).
Dim myStack As New System.Diagnostics.StackTrace()

mySTack.GetFrame(1).GetMethod.Name

You'll have to experiment a little (and perform some checks) but the
StackTrace keeps all methods that are currently executed.

Hope this helped.
 
V

vsr

Great , it works.is there anyway to dynamically get the parameters value, in
the StackTrace i can only get the parameter names and types but not the
values rite?is there any way to get the values?
 
W

Willy Van den Driessche

I have no idea how but I have no doubt it is possible.

However, if you need stuff like that you probably need to rethink your
design. It's OK for generic stuff to go the reflection tour but there is
nothing that can beat the performance of direct parameters and members.
 
V

vsr

Its only in some unusual cases we use this , its definetely not for the
regular coding.
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top