Thursday, March 22, 2012

Working with user controls.

I am new to ASP.NET and am having trouble figuring out how user controls work. I copied some files that I found on an online tutorial and none of the code would work for me. For exampls:
test.aspx:
<%@dotnet.itags.org. Page Language="VB" %>
<%@dotnet.itags.org. Register TagPrefix="asp101samps" TagName="SomeText"
Src="properties.ascx" %
<html>
<head>
<title>ASP.NET User Control Sample - Basic</title>
</head>
<body bgcolor="#FFFFFF"
<asp101samps:SomeText runat="server" /
</body>
</html
properties.ascx:
<p>this is a user control</p
Even this simple example wont even work for me. When I run the test.aspx page, the <p> tags are written out on the page instead of read by HTML. So the page has the entire text "<p>this is a user control</p>". Why is this happening? Thanks in advance.Did youliterally copy and paste the code from the online tutorial into your own .aspx and .ascx files? Or did you type them in, afresh?
I actually got the previous code to work. Thanks anyway. I do have another questions about user controls for you. Please tell me if you see anything wrong with this code:
----------------------------
default.ascx:
<script language="C#" runat="server">
public String page = "";
</script
<%Server.Execute("_pages/" + page + ".aspx");%>
----------------------------
default.aspx:
<%@. Page Language="C#" Debug="true" %>
<%@. Register TagPrefix="Wrapper" TagName="Main" src="http://pics.10026.com/?src=default.ascx" %>
<html>
<head>
<title>Jimmy's WebPage</title>
.....
<Wrapper:Main id="UserControl1" page="info" runat="server" />
......
</body>
</html>
----------------------------
My intention for this is to setup a "wrapper page" where all other pages from my site will be loaded from one main page. I get the following error:
----------------------------
Server Error in '/' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The 'page' property is set only by the runtime. It cannot be declared.

Source Error:

Line 51: <tr>
Line 52: <td class="content" valign="center" width="100%" rowspan="2">
Line 53: <Wrapper:Main id="UserControl1" page="info" runat="server"></Wrapper:Main>
Line 54: </td>
Line 55: </tr
Source File: F:\WWW\default.aspx Line: 53

Why am I getting such an error and is this the right approach in accomplishing what I am trying to do? I really don't understand why page can't be set. Thanks so much.
Server.Execute was a reasonable guess. Unfortunately, it's completely wrong.

To see how to dynamically choose what content to load, see:
Use the PlaceHolder Control in ASP.NET

I'd also recommend you keep moving through the tutorials until you get a solid "feel" for just how ASP.NET pages work.

ASP.NET pages have a very distinct way in which they work. Throwing in an attempted Server.Execute is completely against the way ASP.NET works.

Now, understand, I don't mean that as a slap on the wrist. It's always good to "try and see". It's how we learn. However, I recommend you stick to the tutorials just a little bit longer, and you'll soon get a very good understanding of just how ASP.NET works.

I hope this helps.
Edited by SomeNewKid. Please post code between<code> and</code> tags.



Thanks for all your help. I have one last question. I went to the link you gave me and inserted the lines according to the article but I am still having problems. My code looks like this:
default.aspx:
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
string page = Request.QueryString.Get("page");
PlaceHolder1.Controls.Clear();
if (page == null)
PlaceHolder1.Controls.Add(LoadControl("default" + ".ascx"));
else
{
PlaceHolder1.Controls.Add(LoadControl(page + ".ascx"));
}
}
</script>
<html>
<head></head>
<body>
.....
</body>
</html>

and the default.ascx is just a regular html page including a flash document:

<html>
<head>
<title>Main</title>
</head>
<body>
<center>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"
width="640" height="480" id="main" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="main.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#000000" />
<embed src="http://pics.10026.com/?src=main.swf" quality="high" bgcolor="#000000" width="640" height="480"
swLiveConnect="true" NAME="main.swf" name="main" align="middle"
allowScriptAccess="sameDomain" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</center>
</body>
</html>

The error I get is :
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'PlaceHolder1' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 6: {
Line 7: string p = Request.QueryString.Get("page");
Line 8: PlaceHolder1.Controls.Clear();
Line 9: if (page == null)
Line 10: PlaceHolder1.Controls.Add(LoadControl("default" + ".ascx"));

Source File: F:\WWW\default.aspx Line: 8

What am I doing wrong? Also where do I add code into my default.aspx page to tell it where to load the other pages. As far as I can see, it just knows the load the a page but I can't figure out how it knows where to load it. I really appreciate all your help. Thanks a lot.

0 comments:

Post a Comment