asp.net - beginner - why can't my class create controls on my page?

R

Ranginald

Hi,

My goal is to have a class named class1 that will render a label
control on whatever page needs it. I want to instantiate class1 from
whatever page, and then call the createLabel() method defined in
class1.

My ultimate goal is to use this structure as a template for adding
dropdownlists (but I want to understand a simple example first).

In the following basic code, the code compiles but I cannot get the
label to display on the web page. I don't want to touch the design
view and I want to do it all in code.
Any suggestions are greatly appreciated.
---------------
class1.cs
----------------
namespace Lefantze
{
using...
public class Class1: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label label1;
public Class1()
{
}
public void createLabel()
{
label1 = new Label();
label1.EnableViewState = true;
this.Controls.Add(label1); <-- I tried this
base.Controls.Add(label1); <-- I tried this
label1.Text = "This is from Class1, createLabel()";
}
}
}

--------------
default2.aspx.cs
--------------
using.....
using Lefantze;

public partial class Default2: Class1
{
protected void Page_Load(object sender, EventArgs e)
{
Class1 rangy = new Class1();
rangy.createLabel();
}
}


Thanks!
 
R

rjl

I want to do the same. you have to add at least the declaration on one
line in asp page. also, I have better luck adding the control to a
panel object for placement.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top