Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Finding a control in codebehind.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Finding a control in codebehind.

 Post Reply Post Reply
Author
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post Topic: Finding a control in codebehind.
    Posted: 22 August 2005 at 8:23am
Hi all,
 
not sure how often this question has been answered, if at all. 
 
I have written a webcustomcontrol (NOT ascx). 

I have 10 on a web page and want to find them  in the code behind.

I have tried using foreach, but I am not sure what  collection to search in. 

Does anyone know the answer to this question? Is it possible?

Thanks in advance,


Edited by Leeb65 - 22 August 2005 at 8:24am
Lee


Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 22 August 2005 at 9:34am
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(YourControl))
{
Response.Write(c.Name);
}
}
Back to Top
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2005 at 7:46am

I get this error:

Object does not match target type.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.TargetException: Object does not match target type.

Source Error:

Line 71: 						if(y.Name == "EditMode")
Line 72: 						{
Line 73: 							y.SetValue("EditMode", bEdit, null);
Line 74: 						}
Line 75: 					}

Source File: d:\www\smdtest\default.aspx.cs    Line: 73
 
From this code:
 

Type t;

foreach (Control oPart in this.Controls)

{

t = typeof(SMDCMS.WebPart);

if (t.FullName.IndexOf("WebPart")>0)

{

PropertyInfo[] p = t.GetProperties();

foreach(PropertyInfo y in p)

{

if(y.Name == "EditMode")

{

y.SetValue("EditMode", bEdit, null);

}

}

}

}

 
Please help!!
 
Lee


Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2005 at 10:17am
much more compact and doesn't use reflection (NEVER use reflection in a loop like that just to set properties - it is as slow as hell). Just use boxing to convert the type


foreach (Control oPart in this.Controls)
{
    if (oPart.GetType() == typeof(SMDCMS.WebPart))
    {
        SMDCMS.WebPart webPart = oPart as SMDCMS.WebPart;

        webPart.EditMode = true;
    }
}



Edited by Mart - 23 August 2005 at 10:23am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.