Building User Contols in ASP.NET

S

Steve Gelfmann

I am new to ASP.NET. So I am trying to learn from the book I just
bought. There is a simple example of how to build a user control
which I religiously typed into the Notepad, but the results are not
what was expected.
Here's what I did.
First, I created the user control and saved it in a file
"SimpleHeader.ascx".

<%@ Control Language="VB" %>
<html>
<head><title>Simple User Control</title></head>
<h2>This is an example of a simple user control</h2>
<i>Simple Header!</i>
<hr />

Then, I created an ASP.NET page called HomePage.aspx that included the
control.

<%@ Page Language="VB" %>
<%@ Register TagPrefix="SimpleUserControl" TagName="Header"
Src="SimpleHeader.ascx" %>
<SimpleControl:Header
id="ctlHeader"
runat="server" />
Hello!
</body>
</html>

I stored both files in the same folder. When I try to view the page it
just shows: Hello! with no header.

What do I do wrong? How to make user control to appear on the page?
Thanks.
 
J

Jos

Steve said:
I am new to ASP.NET. So I am trying to learn from the book I just
bought. There is a simple example of how to build a user control
which I religiously typed into the Notepad, but the results are not
what was expected.
Here's what I did.
First, I created the user control and saved it in a file
"SimpleHeader.ascx".

<%@ Control Language="VB" %>
<html>
<head><title>Simple User Control</title></head>
<h2>This is an example of a simple user control</h2>
<i>Simple Header!</i>
<hr />

Then, I created an ASP.NET page called HomePage.aspx that included the
control.

<%@ Page Language="VB" %>
<%@ Register TagPrefix="SimpleUserControl" TagName="Header"
Src="SimpleHeader.ascx" %>
<SimpleControl:Header
id="ctlHeader"
runat="server" />
Hello!
</body>
</html>

I stored both files in the same folder. When I try to view the page it
just shows: Hello! with no header.

What do I do wrong? How to make user control to appear on the page?
Thanks.

With the exact code as above, I get an error.
When I change the prefix SimpleControl into SimpleUserControl,
it works fine.
 
S

Steve Gelfmann

You are absolutely right about that. I think I tried to solve this
problem by altering the files so many times that I missed this error.
However, this was not the problem. I have a feeling that may be
something beyond following the syntax rules is what's wrong. Maybe I
should reinstall .NET Framework or enable/disable some security
options. But I don't know which direction to go. Do you?

Thanks

P.S. Just to be on a safe side here's the current version of the files
(exact version from the book). When I open the page in the browser it
shows the contents of the HomePage.aspx file.

SimpleControl.ascx
___________________
<html>
<head><title>SimpleHeader.ascx</title></head>
<body>
<h2>This is an example of a simple user control</h2>
<i>Simple Header!</i>
<hr>

HomePage.aspx
______________
<%@ Register TagPrefix="SimpleHeader" TagName="Header"
Src="SimpleHeader.ascx"
%>
<SimpleHeader:Header id="ctlHeader" runat="server" />
Hello!
</body>
</html>
 
S

Steve Gelfmann

You are absolutely right about that. I think I tried to solve this
problem by altering the files so many times that I missed this error.
However, this was not the problem. I have a feeling that may be
something beyond following the syntax rules is what's wrong. Maybe I
should reinstall .NET Framework or enable/disable some security
options. But I don't know which direction to go. Do you?

Thanks

P.S. Just to be on a safe side here's the current version of the files
(exact version from the book). When I open the page in the browser it
shows the contents of the HomePage.aspx file.

SimpleControl.ascx
___________________
<html>
<head><title>SimpleHeader.ascx</title></head>
<body>
<h2>This is an example of a simple user control</h2>
<i>Simple header!</i>
<hr>

HomePage.aspx
______________
<%@ Register TagPrefix="SimpleHeader" TagName="Header"
Src="SimpleHeader.ascx"
%>
<SimpleHeader:Header id="ctlHeader" runat="server" />
Hello!
</body>
</html>
 

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

Forum statistics

Threads
473,798
Messages
2,569,651
Members
45,384
Latest member
GOLDY

Latest Threads

Top