Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Obtaining Correct Type with usage of ParseControl()

Reply
 
Old 09-24-2008   #1 (permalink)
Jordan S.


 
 

Obtaining Correct Type with usage of ParseControl()

The following 3 lines of code exist in a user control (Page_Load). Line 3
chokes with the following exception message: "Unable to cast object of type
'System.Web.UI.Control' to type 'System.Web.UI.WebControls.PlaceHolder'."

Why is this? How can I get a proper "PlaceHolder" reference to ctl?

line1: string sTemp = "<asp:PlaceHolder ID=\"MyPH\"
runat=\"server\"></asp:PlaceHolder>";

line2: Control ctl = this.ParseControl(sTemp);

line3: System.Web.UI.WebControls.PlaceHolder px =
(System.Web.UI.WebControls.PlaceHolder)ctl;

fwiw: I'm using .NET 3.5.

Thanks.




My System SpecsSystem Spec
Old 09-25-2008   #2 (permalink)
bruce barker


 
 

Re: Obtaining Correct Type with usage of ParseControl()

why use parse control for this? just create the control, its much better
coding.

var px = new System.Web.UI.WebControls.PlaceHolder();
px.ID = "MyPH";
this.Controls.Add(px);

you original problem is parse control returns user control containing
the placeholder as a child control.

-- bruce (sqlwork.com)


Jordan S. wrote:
Quote:

> The following 3 lines of code exist in a user control (Page_Load). Line 3
> chokes with the following exception message: "Unable to cast object of type
> 'System.Web.UI.Control' to type 'System.Web.UI.WebControls.PlaceHolder'."
>
> Why is this? How can I get a proper "PlaceHolder" reference to ctl?
>
> line1: string sTemp = "<asp:PlaceHolder ID=\"MyPH\"
> runat=\"server\"></asp:PlaceHolder>";
>
> line2: Control ctl = this.ParseControl(sTemp);
>
> line3: System.Web.UI.WebControls.PlaceHolder px =
> (System.Web.UI.WebControls.PlaceHolder)ctl;
>
> fwiw: I'm using .NET 3.5.
>
> Thanks.
>
>
>
My System SpecsSystem Spec
Old 09-25-2008   #3 (permalink)
Jordan S.


 
 

Re: Obtaining Correct Type with usage of ParseControl()

Thanks Bruce... the piece I was missing was that ParseControl returns a user
control that in turn contains the PlaceHolder as a child. That's helpful to
know.

I'm using ParseControl for this because I have an HTML string that contains
one or more PlaceHolder controls, and I was having difficulty getting to the
correct collection. The shortened HTML snippet in the OP was just for the
sake of brevity.

Thanks again.



"bruce barker" <nospam@xxxxxx> wrote in message
news:O3mwOhsHJHA.1160@xxxxxx
Quote:

> why use parse control for this? just create the control, its much better
> coding.
>
> var px = new System.Web.UI.WebControls.PlaceHolder();
> px.ID = "MyPH";
> this.Controls.Add(px);
>
> you original problem is parse control returns user control containing the
> placeholder as a child control.
>
> -- bruce (sqlwork.com)
>
>
> Jordan S. wrote:
Quote:

>> The following 3 lines of code exist in a user control (Page_Load). Line 3
>> chokes with the following exception message: "Unable to cast object of
>> type 'System.Web.UI.Control' to type
>> 'System.Web.UI.WebControls.PlaceHolder'."
>>
>> Why is this? How can I get a proper "PlaceHolder" reference to ctl?
>>
>> line1: string sTemp = "<asp:PlaceHolder ID=\"MyPH\"
>> runat=\"server\"></asp:PlaceHolder>";
>>
>> line2: Control ctl = this.ParseControl(sTemp);
>>
>> line3: System.Web.UI.WebControls.PlaceHolder px =
>> (System.Web.UI.WebControls.PlaceHolder)ctl;
>>
>> fwiw: I'm using .NET 3.5.
>>
>> Thanks.
>>
>>
>>
>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Sleep mode correct usage? General Discussion
auto-correct as you type Vista mail
Obtaining RC2 Vista General
Obtaining a new key Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46