A
Alan Silver
Hello,
I have a user control which I am trying to load dynamically, but am
running into problems. I think the problem is because I have two .ascx
files that refer to the same .ascx.cs file.
A simple example is like this...
Ferret1.ascx
=========
<%@ Control Language="C#" CodeFile="Ferret.ascx.cs" Inherits="Ferret" ClassName="Ferret" %>
One - <asp:Literal ID="litFerretName" Runat="Server" />
I have another one Ferret2.ascx which is exactly the same, except for
the fact that the "One" is changed to "Two" so I can see which is which
on the page.
The code-behind file looks like this...
Ferret.ascx.cs
===========
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class Ferret : UserControl {
public void DisplayFerret() {
litFerretName.Text = "hello";
}
}
Now, in the calling page, I have a Placeholder called plcFerret. The
code in the code-behind for the page looks like this...
Ferret ctlFerret1 = (Ferret)LoadControl("Ferret1.ascx");
ctlFerret1.DisplayFerret();
plcVole.Controls.Add(ctlFerret1);
Ferret ctlFerret2 = (Ferret)LoadControl("Ferret2.ascx");
ctlFerret2.DisplayFerret();
plcVole.Controls.Add(ctlFerret2);
The first control loads fine, but the second one gives an error of
"Unable to cast object of type 'ASP.Ferret' to type 'Ferret'" on the
line where it tries to load Ferret2.ascx.
Any ideas? This has been driving me mad!! TIA
I have a user control which I am trying to load dynamically, but am
running into problems. I think the problem is because I have two .ascx
files that refer to the same .ascx.cs file.
A simple example is like this...
Ferret1.ascx
=========
<%@ Control Language="C#" CodeFile="Ferret.ascx.cs" Inherits="Ferret" ClassName="Ferret" %>
One - <asp:Literal ID="litFerretName" Runat="Server" />
I have another one Ferret2.ascx which is exactly the same, except for
the fact that the "One" is changed to "Two" so I can see which is which
on the page.
The code-behind file looks like this...
Ferret.ascx.cs
===========
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class Ferret : UserControl {
public void DisplayFerret() {
litFerretName.Text = "hello";
}
}
Now, in the calling page, I have a Placeholder called plcFerret. The
code in the code-behind for the page looks like this...
Ferret ctlFerret1 = (Ferret)LoadControl("Ferret1.ascx");
ctlFerret1.DisplayFerret();
plcVole.Controls.Add(ctlFerret1);
Ferret ctlFerret2 = (Ferret)LoadControl("Ferret2.ascx");
ctlFerret2.DisplayFerret();
plcVole.Controls.Add(ctlFerret2);
The first control loads fine, but the second one gives an error of
"Unable to cast object of type 'ASP.Ferret' to type 'Ferret'" on the
line where it tries to load Ferret2.ascx.
Any ideas? This has been driving me mad!! TIA