How do I CAST a control?

  • Thread starter Thread starter Johnny Luner
  • Start date Start date
J

Johnny Luner

I have a control on my page, but I don't know how to CAST it so that I can
access the properties dynamically using FindControl... help. Is it
possible?? since control is not a real class. If not, any solutions
suggestion? Here is the code:


<%@ Register TagPrefix="company" tagname="items" src="items.ascx"%>

<script language="c#" runat="server">

void Page_load(Object sender, EventArgs e) {

// This one works
item_1.name = "test";
item_2.name = "test";

// I don't know how to get it working
for (int i=0; i<=2; i++) {
((??HOW TO CAST??) FindControl("item_" + i)).name = "test";
}

}

</script>


<company:items id="item_1" runat="server" />
<company:items id="item_2" runat="server" />
 
Casting is as simple in C# as putting the type name of the desired cast into
parentheses in front of the casted object. Example:

(System.Object) SomeControl // Casts SomeControl as a System.Object

It looks like your problem is that you're trying to find the Control by its
name attribute, and ASP.Net uses the ID of the Control to find it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
I know (Textbox) will work because it I can the aspx page knows about it.
But mine is a custom control, not compiled as dll.. it is being registered
as:
<%@ Register TagPrefix="company" tagname="items" src="items.ascx"%>

I have tried all these and doesn't work:

((company.items) FindControl("item_" + i)).name = "test";
((items) FindControl("item_" + i)).name = "test";

Any idea?

Thanks..
 
Use the fully-qualified name (including the namespace).

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
What is the class name of this object?, you'll want to be using that and not
the prefix/tagname you define in that tag. You may need to compile this
control to use it in compiled code though.
 
Back
Top