Visual Studio/ASP.net and C# confusion

R

Ranginald

I have a page, default.aspx.cs that instantiates a label, label1 and
adds some properties to the label:

Label Label1 = new Label();
this.Controls.Add(Label1);
Label1.BackColor = System.Drawing.Color.Pink;
Label1.Text = "Lefantze test: _Default class";

===============
On the default.aspx page, the html is as follows:

<asp:Label ID="Label1" Text="Ranginald" BorderColor="Gold"
runat="server" Width="266px" BorderWidth="10px"></asp:Label></div>
</form>

When I compile and run the page, I have two textboxes -- and they are
both label1.
Why is this happening? I thought there could be only one control with
the name label1?
(Note that in design view, only one label is shown and that's the one
in the html)

Thanks!
 
V

V

Hi,

1. When you run, you should have 2 Labels (not two text boxes).
2. You are supposed to have 2 text boxes. One is created because of the
code in the html part of your page at design time, and the other is
created because of the code you wrote in the code-behind.
3. The reason for why you apparently have two controls both called
'Label1' is that when you create a control from code (at runtime), the
ID is auto-generated. The Label1 in your code is just the variable name
and not the ID.
4. When you page is running, try to do a view html source and then you
will notice that the IDs for both the controls are actually different.

Regards,
Vaibhav
 
R

Ranginald

Thanks for your response.

1. Is it possible to add properties to a control, say Label2,
programatically as well as via the properties window in design view.

2. If I have a method, createTextBox() that is called in the Page_Load
of default.aspx, and has an onClick method of doSomething(), what is
the best way to inherit this methoed createTextBox() method into
another page with another class.

I can do it with a label without a problem, but everything seems to
change with a control such as a listbox.

Thanks again.
 
V

V

Hello,

My responses below:
1. Yes, it is possible to add properties to a control programmatically
as well as via the properties windows. However, there are certain
properties which can only be changed at design time and others which
can only be changed at run-time. You need to look up the documentation
for whichever class/control you are working with.

2. I am not too sure what you are trying to do here. If you want to
dynamically add a control to your page and reuse the code you have
written to do that, then yes you can. I will need some more explanation
of what you are trying to achieve.

Regards,
Vaibhav
 
R

Ranginald

Here's the plan:

I am working on a project and I want to have two drop down lists on the
left side of the page. These are ddlCategory and ddlSculpture. On the
OnSelectedIndexChanged the controls head to runCategory() and
runSculpture() respectively.

This area is to be locked with a template so I can reuse this
navigation on all pages.

The content section of each page (basically a big editable table in the
middle) will obviously be different for each page. In the
sculpture.aspx page, for example, in the body section there is a
datagrid that does its own thing and has its own code.

I was told that I should try to use inheritance so that each page can
easily use the template, but after weeks of struggling, I have
unfortunately resigned to just copying the code for the drop down lists
and their executing methods to each page of the site.

I've made a lot of posts, and the post above was to try and get a
simplified model going so I could try to make sense of all of this. I
tried to do it with each page having a separate .cs page, but had
namespace issues. Then tried to put all the code into one .cs page
with different classes to keep things clean. I then run into
inheritance trouble.

I know it defeats the whole purpose, but at least at this point all of
the pages have their own .cs code file so the code is still separate
from the presentation layer. There are only seven or do pages in the
whole site, three of which are dynamically generated from a MSSQL
database.

If you have the time and want to take a peek, I've posted the code
elsewhere:

in microsoft.public.dotnet.languages.csharp --
Beginner - basic inheritance problem -- public/protected

The whole code is a few post down.

Thanks in advance for your help so far. I really appreciate it!
 
V

V

Hello,

I have been away (a lot of work in the office).... If you are still at
the same point in your problem, please let me know, then i will start
working with you again.

Regards,
Vaibhav
 
R

Ranginald

Thanks. As it is I am still at the same point. Any help is greatly
appreciated.

-David
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top