aspx thread: Re: [aspngfreeforall] Yupp! It looks Like A BUG! -- FW: Beta 1 Bug or Just Me! Can't Ad d a Con trol Dynami cally
Scott,
I can tell you that this can be done. Not being that fluent in C#, I have
been unable to successfully translate my VB example. Maybe you (or someone
else) can translate it =). Some of this code came from an example at
aspfree.com. Here is the code I use to achieve this kind of dynamic
functionality:
----- uctest.aspx --------
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Reflection" %>
<script language="VB" runat="server">
Public Sub Page_Load(Src As Object, E As EventArgs)
'Load the control
Dim myUC as UserControl = LoadControl("ucContent.ascx")
'Set the Usercontrol Type
Dim ucType as Type = myUC.GetType()
'Get access to the property
Dim ucDefualtSidProperty as PropertyInfo
ucType.GetProperty("DefualtSid")
' Set the property
ucDefualtSidProperty.SetValue(myUC,1,Nothing)
pnlTest.Controls.Add ( myUC )
End Sub
</script>
<asp:panel id="pnlTest" runat="server" />
------ ucContent.ascx --------
<script language="VB" runat="server">
Private _DefualtSid as Integer = 0
Public Property DefualtSid As Integer
Get
Return _DefualtSid
End Get
Set
_DefualtSid = value
End Set
End Property
</script>
<h1><%=_DefualtSid%></h1>
hth,
Alex
http://www.asp-grandrapids.net
webmaster@a...
----- Original Message -----
From: "Scott Watermasysk" <swatermasysk@C...>
To: "aspngfreeforall" <aspngfreeforall@l...>
Sent: Wednesday, May 30, 2001 9:12 AM
Subject: [aspngfreeforall] Yupp! It looks Like A BUG! -- FW: Beta 1 Bug or
Just Me! Can't Ad d a Con trol Dynami cally
> I have gotten some feed back about my post last night from the p2p lists
and
> it does look like it might be a bug. (I am impatient so I posted it both
> here and on p2p!)
>
> Has anyone elsed experienced this problem? Or come up with an alternative
> solution?
>
> Scott
>
> Message From p2p:
>
> -----Original Message-----
> From: Alex Homer
> Sent: 30 May 2001 11:36
> To: 'ASP+'
> Subject: RE: [aspx] Alex or anyone: Beta 1 Bug or Just Me! Can't Add a
> Control Dynami cally
>
>
> I've spoken to a colleague who had a similar problem, and it looks like a
> bug in Beta 1. From tests on later intermediate builds it is working fine
> and so your code should be OK for Beta 2. I'm waiting to see if anyone has
> found a fix for Beta 1 yet, and if so I'll post it here.
>
>
>
>
> Original Message:
>
> Good Night All,
>
>
> I have a user control I would like to add to a page dynamically and set
two
> public properties.
>
> In the SDK Framework Documentation the following example is given:
>
> Control c1 = LoadControl("MyUserControl.ascx");
> ((MyUserControl_ascx)c1).BackColor = "beige";
> Page.Controls.Add(c1);
>
>
> I have tried this with my own control "ucContent.ascx" and it works, but I
> can not have access to my public properties.
>
> The following works:
>
> Control c1 = LoadControl("ucContent.ascx");
> Page.Controls.Add(c1);
>
> However, when I try to access on of the controls public properties
> (DefualtSid and DefualtCid) I get an error stating "CS0234: The type or
> namespace name 'ucContent_ascx' does not exist in the class or namespace
> 'ASP.dynamiccontrol_aspx'" (Sample page is called
ASP.dynamiccontrol.aspx).
>
> The following does not work:
> Control c1 = LoadControl("ucContent.ascx");
> ((ucContent_ascx)c1).DefualtSid = 1;
> Page.Controls.Add(c1)
>
> I have tried many different syntax formats, but nothing seems to work. In
> addition, I am copying directly from the SDK, so I would think this would
be
> no problem.
>
> Second Example:
>
> I also tried to access the properties using c1.DefualtSid = 1;, but I got
an
> error saying "CS0117: 'System.Web.UI.Control' does not contain a
definition
> for 'DefualtSid'.
>
> Furthermore, I am sure it is not my control, when I use <%@ Register
> TagPrefix = "3A" TagName = "Content" src = "ucContent.ascx" %> and
> <3A:Content id = "Content" DefualtSid = "0" DefualtCid = "0" runat
> "Server" /> to access and set the defualts I have no problems, it only
when
> I try to set them dynamically.
>
> Is this a bug in Beta 1? Am I just going Nuts? Why me?
>
> Has anyone gotten this to work? If not, any suggestions.
>
>
> Finally, I tried to do this at both the Page_Load and Page_Init levels,
> although I believe Page_Init is more appropriate.
>
> Thanks again to everyone on this list. Your feedback in the past has been
> great.
>
> Regards,
> Scott
>
> | [aspngfreeforall] member alowe@s... = YOUR ID
> | http://www.asplists.com/asplists/aspngfreeforall.asp = JOIN/QUIT