J
John Mandia
Hi All,
I’ve been looking into creating a partial class for a usercontrol but
without luck. The ascx.cs class sees the method in the partial class and the
partial class can see methods in the ascx.cs file (via intellisense) but when
I do a build it says e.g. CallPartialMethod does not exist in the current
context.
I’ve added some quick sample code below to show what I mean.
e.g.
test.ascx
// standard usercontrol
…html
<asp:label id=â€lblTestLabel†runat=â€serverâ€>Welcome to my test</asp:label>
...html
test.ascx.cs (pointed to by codefile attribute in ascx)
public partial class MyTestUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Works and shows up in intellisense
string test = lblTestLabel.Text;
string test2 = CallPartialMethod();
}
}
partialtest.cs
public partial class MyTestUserControl
{
public string CallPartialMethod()
{
// lblTestLabel shows up as a field (of the proper type) but I
// have to cast it in order to access the
..Text
// Property (well it doesn’t show it via
intellisense. I’ve tried to
// see if it would still build without
casting but
// receive the error lblTestLabel does not
exist in the current
// context).
return lblTestLabel.Text;
}
}
It’s as if Visual Studio sees the methods etc as that is how partial classes
are supposed to work but they do not get included when it comes to building
usercontrols (And most probably pages). I tried pointing the codefile setting
to partialtest.cs (Had to make the class inherit from usercontrol) and it
built fine (Since it didn’t have any references to test.ascx.cs and it isn’t
calling any methods from that class) but it still doesn’t include more than
one cs file.
Has anyone achieved partial classes with usercontrols or come across this
problem before?
Thanks,
John
I’ve been looking into creating a partial class for a usercontrol but
without luck. The ascx.cs class sees the method in the partial class and the
partial class can see methods in the ascx.cs file (via intellisense) but when
I do a build it says e.g. CallPartialMethod does not exist in the current
context.
I’ve added some quick sample code below to show what I mean.
e.g.
test.ascx
// standard usercontrol
…html
<asp:label id=â€lblTestLabel†runat=â€serverâ€>Welcome to my test</asp:label>
...html
test.ascx.cs (pointed to by codefile attribute in ascx)
public partial class MyTestUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Works and shows up in intellisense
string test = lblTestLabel.Text;
string test2 = CallPartialMethod();
}
}
partialtest.cs
public partial class MyTestUserControl
{
public string CallPartialMethod()
{
// lblTestLabel shows up as a field (of the proper type) but I
// have to cast it in order to access the
..Text
// Property (well it doesn’t show it via
intellisense. I’ve tried to
// see if it would still build without
casting but
// receive the error lblTestLabel does not
exist in the current
// context).
return lblTestLabel.Text;
}
}
It’s as if Visual Studio sees the methods etc as that is how partial classes
are supposed to work but they do not get included when it comes to building
usercontrols (And most probably pages). I tried pointing the codefile setting
to partialtest.cs (Had to make the class inherit from usercontrol) and it
built fine (Since it didn’t have any references to test.ascx.cs and it isn’t
calling any methods from that class) but it still doesn’t include more than
one cs file.
Has anyone achieved partial classes with usercontrols or come across this
problem before?
Thanks,
John