Originally posted by pippo
I would not say that
"That is very nice spearation of HTML and code"
Oh, this is my fault for not pointing this out.. You do not have to put the code in <script> tags. In fact, that's stupid to do, for the very reason that it mixes code and HTML. The reason I used it at all is that it's very good for demonstrating code, due to it's simple syntax. A "real" aspx page has something like this on top:
<%@ Page language="c#" Codebehind="index.aspx.cs" Inherits="NameSpace1.Class2" %>
... that's called CodeBehind.
Another obversation is that you are using "asp tags" to generate html tags.
I would prefer to use "custom" or "asp" tags to do "logic" such as an iterator than generating html code.
Doing that you have some html tags written by you and some that are inside custom "asp tags".
Actually, you don't have to use <asp: tags for HTML content. ASP.NET offers controls for standard HTML controls, too. All you need to do is add runat="server" in them, and you have access to them in a similiar way I used <asp: tags in the examples below. For instance:
Code:
<table id="Table1" CellPadding=5 CellSpacing=0 Border="1" runat="server" />
Now, to add a row to this table, we would do this:
Code:
HtmlTableRow r = new HtmlTableRow();
for (int i=0; i<numcells; i++) {
HtmlTableCell c = new HtmlTableCell();
string CellText = "row " + j.ToString() + ", cell " + i.ToString();
c.Controls.Add(CellText);
r.Cells.Add(c);
}
r.Cells.Add(c);
Table1.Rows.Add(r);
Note that if we do this codebehind, the ONLY changes done to the .aspx page is the addition of the Codebehind tag on top, and the addition of runat="server" attributes. That is very nifty.
While I agree that custom tags should mostly be used for looping and such, custom tags may have their place, because you can easily define your own tags, called User Controls. The <asp: tags are really just User Controls that are pre-made by MS. It's very easy to define special tags like this:
Goes on top:
Code:
<%@ Register TagPrefix="Pippo" TagName="Message" Src="pippo.ascx" %>
Markup goes like this:
Code:
<Pippo:Message id="PippoMessage" MessageText="This is a custom message!" Color="blue" runat="server"/>
code in pippo.ascx goes like this:
Code:
<script language="C#" runat="server">
//Note that this code can also be "CodeBehinded",
//just like in the earlier example.
public String Color = "blue";
public String Text = "This is a simple user control!";
</script>
<span id="Message" style="color:<%=Color%>"><%=Text%></span>
Of course, that can be MUCH more advanced, such as specific generation tags for <forum:post> or why not <sitepoint:editize> tags.
Bookmarks