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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 ([email protected], 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.
 

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

Back
Top