error inheriting web control - c#

I am trying to add a new custom web control to my asp.net application.
Here is the code for my custom control
using System.Web.UI.WebControls;
namespace MyPersonalCareHealthRecord.classes.extended
{
public class TableHeaderDataCell : TableHeaderCell
{
public string MappedDataField { get; set; }
}
}
In my markup I am using the following code
<dt:TableHeaderDataCell CssClass="HeaderField" runat="server">Test1</dt:TableHeaderDataCell>
After getting some errors, doing some research, and reading this article
http://msdn.microsoft.com/en-US/library/1e9b4c9f(v=vs.80).aspx (along with referencing a book I have on asp.net) I tried adding in a register directive like this one and adjusted my markup to the above.
<%# Register TagPrefix="dt" TagName="TableHeaderDataCell" namespace="MyPersonalCareHealthRecord.classes.extended" %>
However on trying to load the page I get an error: "The directive is missing a 'src' attribute."
I tried changing my directive to:
<%# Register TagPrefix="dt" TagName="TableHeaderDataCell" src="~/Classes/TableHeaderDataCell.cs" %>
and then got error: "The file 'src' is not a valid here because it doesn't expose a type."
After further research and finding this stackoverflow article How to use a Subclassed Control on an ASP.NET Page?
I tried changing my register directive to
<%# Register TagPrefix="dt" TagName="TableHeaderDataCell" namespace="MyPersonalCareHealthRecord" assembly="MyPersonalCareHealthRecord" %>
which resulted in error: "The directive is missing a 'src' attribute."
Adding the src attribute in gave me error: "The 'namespace' attribute is not supported on this directive when a 'tagname' attribute is present."
Removing the tagname attribute gave me errors and removing the namespace tagname did as well....
I realize this was a lengthy question but can anyone help me??? I don't know what I am doing wrong.

Go to add a Webforms User Control.
Once you add it go to code behind of the control (the cs file) and you can put your property there
public string MappedDataField { get; set; }
Save it.
Go to solution explorer and simply drag it in your web form.
In your webform code behind you can access that property.
MyControlName.MappedDataField = "whatever text";

Ok, I believe I have solved it.... I followed the instructions at this link:
http://msdn.microsoft.com/en-us/library/aa310915(v=vs.71).aspx
I created a .dll from my custom control source code file and then made sure the .dll was in the /bin directory in my project. changed my register directive to be:
<%# Register TagPrefix="dt" Namespace="MyPersonalCareHealthRecord.classes.extended" Assembly = "TableHeaderDataCell" %>
(TableHeaderDataCell is the name of the dll file)
At first I got an error about the type being in two places so I had remove the .cs file from the bin directory before it would work. I also had some issues referencing my new type in the actual code behind file so I had to add a reference and browse to the new dll and add a reference to it in my project. After adding the reference I was able to use it in my code behind file.

Related

User control within User control within Master Page and setting a property c#

How do I get/set a property within a User Control inside User Control within Master Page in c# ?
Example structure:
Web User Control 'WebUserControlA.ascx'
Web User Control 'WebUserControlB.ascx'
Master Page 'Content.master'
Web Form Page 'Test.aspx'
'Test.aspx', is a content page from the 'Content.master' page, it includes:
<%# MasterType VirtualPath="~/Content.master" %>
The content.master page includes the 'WebUserControlA.ascx' and in 'WebUserControlA.ascx' is 'WebUserControlB.ascx' included.
I wish to be able to set text of a label control in the 'WebUserControlB.ascx' from the 'Test.aspx' code behind file.
UPDATED NOTES:
After receiving a potential answer from 'VDWWD', which allowed me to set the text label, I need to expand my original question:
"I would like to control a get/set a property within the code behind of the WebUserControlB.ascx so that I'm able to display content from the database correctly, for example I want to pass in a country code from the code behind of test.aspx to the WebUserControlB.ascx code behind"
public string CountryCode { get; set; }
I have tried the following however was not able to get hold of the get/set property:
UserControl WebControlB = (UserControl)Page.Master.FindControl("WebUserControlA").FindControl("WebUserControlB");
if (WebControlB != null) {
WebControlB.CountryCode = "[CODE]";
}
When trying to do this i get the following error:
'UserControl' does not contain a definition for 'CountryCode' and no
extension method 'CountryCode' accepting a first argument of type
'UserControl' could be found (are you missing a using directive or an
assembly reference?)
Thanks for helping

Cannot reference my web usercontrol (ascx) from code behind

I am trying to dynamically create a user controls in Visual Studio 2012/ASP.Net/C# but my project does not recognise the type name of my user control when I try to use it in the code behind of any other aspx or ascx page. I've tried rebuilding it and also removing namespaces and it seems like it should just work but it doesn't!
The top line of my ascx file is this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="AG_Controls_WebUserControl" %>
The corresponding codebehind looks like this:
public partial class AG_Controls_WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
I try to declare in instance of it in the code behind of a blank aspx page like this:
AG_Controls_WebUserControl test = new AG_Controls_WebUserControl();
And I just get :
Error 3 The type or namespace name 'AG_Controls_WebUserControl' could
not be found (are you missing a using directive or an assembly
reference?)
It's been a long day and I am pretty tired but this shouldn't be rocket science, but I can't see what the problem is. Help much appreciated!
You forgot to include the reference to the control in the ASPX page. You still need to include that reference if you are dynamically creating the control in the code behind.
<%# Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

Instantiate a web user control in a class in app_code

I am writing a class called Evaluatieform that works with web user controls, their events and eventually adds them to a panel so the web page only needs to instantiate the Evaluatieform class and call a method that returns the panel so it can show it on the website.
Now the problem is that I can't instantiate my user controls I have defined.
First error : The type or namespace name 'DomeinsCriteriums' could not be found(are you missing a using directive or an assembly reference?)
So the next thing I try is to drag the user controls needed into the app_code folder. This however, does not work because the compiler does not allow a web user control in that folder.
I read somewhere else to use
ASP.webusercontrol_ascx usercontrol = new ASP.DomeinsCriteriums_ascx();
However, this does not work aswell.
Can anybody give me a quick solution for this problem?
All you need to do to load a user control is this.
Control ItemX = (Control)Page.LoadControl("/controls/yourusercontrol.ascx");
However if you are getting a namespace error. Simply add the namespace to the top of your page.
using DomeinsCriteriums;

Can't access master page from user control

My master page code looks something like this:
namespace Recipes
{
public partial class MasterPage : System.Web.UI.MasterPage
{
...
public void UpdateUserLogin()
{
NicknameLbl.Text = ((Recipes.BasePage)Page).CurrentUser.Nickname;
}
...
}
}
I want to call the UpdateUserLogin() method from a user control, something like this:
((Recipes.MasterPage)this.Page.Master).UpdateUserLogin();
But for some reason the compiler doesn't know Recipes.MasterPage (are you missing an assembly blablabla).
Sorry I can't show the exact error message, it's in French.
Maybe the problem is that I added the Recipes namespace around MasterPage manually, it wasn't added by VS.
By the way I'm using VS Web Developer Express 2008.
Do you have any idea how I can make this call work?
Both the MasterPage and the UserControl are child controls of the page they are used by. Your UserControl could potentially be used in a page that doesn't use your MasterPage, and so calling UpdateUserLogin() would not be valid.
You can check it like this, however, and make your call conditionally:
if (Page.Master is MasterPage)
{
((MasterPage)Page.Master).UpdateUserLogin();
}
UPDATE
It seems you were already aware of that, sorry. Your question is about the reference not working. What is the namespace of your UserControl?
I would recommend data binding the NicknameLbl to the CurrentUser.Nickname property. Then the NicknameLbl text will get updated automatically if the property changes.
include a MasterType directive at the top of the Content page ASPX file:
'<%# MasterType virtualpath="~/DetailsMaster.master" %>'
include a public method in the Master page
public void UpdateUserLogin(string value)
{
NicknameLbl.Text = value;
}
access the method from the Content page using the Master syntax:
Master.UpdateUserLogin(Some Text");
http://www.codeproject.com/KB/aspnet/Master_and_Contents.aspx
If your project is a Web Site Project (instead of a Web Application Project), then you do not have a project namespace. All code that is referenced from aspx.cs or master.cs files needs to be stored inside the App_Code directory, as the ASP.Net compiler will create several assemblies instead of just 1, and its not predictable which assembly will contain which aspx code.
Update after 1st comment:
The .ascx.cs and .aspx.cs stay where VS puts them. But it you want to reference classes etc, this needs to be placed inside App_Code, e.g. your Recipes.MasterPage or Recipes.BasePage objects.

How to fix namespace problem with autogenerated Master property if MasterType is set

after weeks of having this issue I finally decided to ask for a solution to the following problem:
In the .aspx page you can set
<%# MasterType VirtualPath="~/Mastername.master" %>
This results in an auto generated property in the .aspx.designer
public new Mastername Master {
get {
return ((Masternamee)(base.Master));
}
}
Works perfectly fine. But if I do changes in the .aspx file, the property will be new auto generated and it looks like the following:
public new NAMESPACE1.Mastername Master {
get {
return ((NAMESPACE1.Mastername)(base.Master));
}
}
Compiling will not be possible afterwards, because the class for the MasterPage cannot be resolved at the given namespace.
The masterpage has NAMESPACE1 as namespace.
Every contentpage has the same NAMESPACE1.
The autogenerated property tries to look for the masterpage class in NAMESPACE1.NAMESPACE1 which will fail, due to it does not exist. Of course I can remove the first NAMESPACE1. to make the app compilable again, but it just sucks to do this nearly every time I make changes in the .aspx file.
Is there a way to avoid this problem? The only way I can think of, is to ignore the auto generated property and make a explicit cast everytime I want have access to the masterpage.
Edit: I'm using Visual Studio 2008 Professional SP1.
For some reason the designer believes that the master page is defined in namespace NAMESPACE1, so look at the master page definition (and code behind) to check its namespace has not been modified (possibly accidentally).
If there is nothing obvious, a search in all files (*.cs, *.aspx, *.master, ...) for NAMESPACE1 may be needed.
(This is where using a VCS would help --- you could check the history of changes.)
Actually it's more a designer "feature". ;-)
The Master name used in your designer file will be pulled from your .Master file's Inherits property. So change how you qualify the Inherits attribute, and that will change the class name used when the designer file is created.
I found a solution that works. I won't use the autogenerated property in the designerfile. I'll write my own wrapper property that I do implement in every contentpage.
I had this same problem when I added <%# MasterType VirtualPath="~/TestMaster.Master" %> to my aspx page in SOURCE view. For some reason, the page never created correctly and kept giving me invalid namespace errors until I actually changed to DESIGN view and resized a control and finally the error went away. Somewhere it was using some cached data (even a Build/Clean Solution didn't clear it out) and until the designer recreates the page, it generates that error.
Change
<%# MasterType VirtualPath="~/Mastername.master" %>
to
<%# MasterType TypeName="Mastername" %>
this will work perfectly

Categories

Resources