!!web control not recognised1 what comes first?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, i am using code behind c# pages. I have put a couple of asp:labels on the
aspx html page. in the code behind i am trying to set them programatically
but am getting error

The type or namespace name 'mylabel' could not be found (are you missing a
using directive or an assembly reference?)

and it has a wiggly line under it. I have saved the aspx file, but cant get
the .cs file to recognise it. What comes first? Or what am I missing? I have
got
using System.Web.UI.WebControls;
at the top of my cs file.

using
 
Your aspx:

<asp:Label id="lblTest" Runat="server"></asp:Label>

Your C#:

using System;
......
using System.Web.UI.WebControls;

namespace SomeSpace
{
public class MyPage : SomeNamespace.BaseClassName
{
protected Label lblTest;

private void Page_Load(...)
{
lblTest.Text = "Some Text";
}
}
}

Compile it and it'll work. Your C# "code-behind" class is the base class for
your aspx html page. Therefore, you have to declare your control in C# class
and since you're not going to use it in any other class make it "protected"
(not "private" or "public") so it'll be visible to your aspx page.

Regards,
Kikoz
 
you didn't give us quite enough code, but it ought to look something like:

<html>
<head></head>
<body>
<form runat="server" id="form1">
<asp:label id="date" runat="server" />
</form>
</body>
</html>

using System.Web.UI.WebControls;

public class WebForm1 : Page{

protected Label date; //are you missing this???

public void Page_Load(...){
date.Text = DateTime.Now.ToShortDateTime();
}
}
 
Are you using VS.NET or someting like Web Matrix, or Dreamweaver??
Note: if you are not using vs.net , then you have to manually compile this
codebehind file first, also referencing the system.web.dll.
 
yes guys it was the declaration of the web control
protected Label mylabel;

THANKS

(Tampa I am using vs.net)

do i have to do this for every control then or is there some way i can get
it to auto do this (think i know answer!)?
 
If some of your functionality repeats on several pages (in several classes)
consider using UserControl. To fully automate it, create basic class
("MyBase", for example), base all classes/pages that use this UserControl on
that MyBase. Declare your control in MyBase the way we did with that lbl.
You'll have to insert control declaration in html on every page (there are
way to avoid it in 1.x version and there are Master Page thing in 2.0
version) and stop using designer. You can use a base class to "auto" declare
any web control the way described above.

Hope it helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top