473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Finding a Control Reference by it's Design-Time ID

I need to be able to get a reference to a Control on an ASPX Page by the
exact ID that I gave that control at Design-Time.

Let's say I created an ASPX that had a control:
<asp:TextBox ID="SomeTB" runat="server" />

Now, I want to be able to discover, at run-time, the ClientID of the
Control. But here's the tough part: I'll only have the string "ID" as it had
been set at Design-Time.

So I tried doing FindControl() but that expects the full, "mangled" run-time
Client ID. And I don't have that because I don't know what it'll be.

Is there no way to get a reference to a Control at run-time based on what
it's DESIGN-TIME ID had been?

Alex
Aug 2 '06 #1
2 1439
Hi Alex,

Since currently FindControl cannot find controls recursively, you need to
following function to find a control by its id which maybe in deeper
hierarchy:

private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}

Then you can use FindControlRecursive(Page, controlID) to find the control
reference. "controlID" will be the ID you assigned at design-time.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 3 '06 #2
You ROCK! Thank you.

Alex

"Walter Wang [MSFT]" wrote:
Hi Alex,

Since currently FindControl cannot find controls recursively, you need to
following function to find a control by its id which maybe in deeper
hierarchy:

private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}

Then you can use FindControlRecursive(Page, controlID) to find the control
reference. "controlID" will be the ID you assigned at design-time.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Watkins | last post by:
I am a novice programer in Vb and I am having a major brain ache finding out which item has been selected within a list view and then either deleting that item or editing them. My searching so...
2
by: john | last post by:
Maybe I haven't had that "a-ha" moment yet, but I think the new approach to web projects is a step in the wrong direction. My main beef is that control over the assembly generation process has...
3
by: Bill Angus | last post by:
I have a collection of controls that represent an address in my project. I wanted to make them into an address control because lots of the business objects I am working with have addresses. I got a...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
2
by: Alex Maghen | last post by:
I want to create a utility function that will seach the current page for one of my UserControls by it's type. So, let's say that I have a UserControl whose class I defined as follows: namespace...
5
by: Fernando Chilvarguer | last post by:
I'm sure this has come up before but I could not find any post on it. How can I read a variable or property that has been set on a ASPX page from inside a ASCX control. ASPX code: public...
5
by: lisa | last post by:
This is incredibly frustrating. I have a VB6 app at work that we're upgrading to .NET. In the VB6 code, I'm able to get a reference to txtText by using frm.Controls(txtText). In ASP.NET, we...
1
by: Mihai Velicu | last post by:
I created a control library and i created a control.I can test it is ok.I added this control to a form .Run the form is ok also.After i modified the control to add some new features and i had some...
7
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've...
5
by: Fred Chateau | last post by:
I am trying to reference a server control in a user control, from the containing page. Is there a way to do that? I have tried "UserControl1.FindControl("ControlName")" but I get a null reference....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.