Monday, March 26, 2012

HTML first, Page_Load later

In the following aspx file the output from Page_Load code is rendered first
and HTML later. I want the other way round.
How can I do that?
<%@dotnet.itags.org. Page Language="C#" %>
<%@dotnet.itags.org. Import Namespace="System.IO" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>Welcome to my Project</title></head>
<body>
<form id="form1" runat="server"><div>
<h3>Below are the files in my Project</h3>
</div></form>
</body></html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e) {
DirectoryInfo dir = new DirectoryInfo(this.MapPath("~"));
foreach (FileInfo f in dir.GetFiles("*.as*x"))
if (f.Name.ToLower() != "default.aspx") Response.Write("<a href='./" +
f.Name + "'>" + f.Name + "</a>" + "<br>");
}
</script>Instead of doing raw Response.Write's to the output stream, try databinding
your "stuff" to a control defined on the Page that is suitable for rendering
such a list - a Repeater, DataList, or Gridview.
Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"dotNET learner" wrote:

> In the following aspx file the output from Page_Load code is rendered firs
t
> and HTML later. I want the other way round.
> How can I do that?
>
> <%@. Page Language="C#" %>
> <%@. Import Namespace="System.IO" %>
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head runat="server"><title>Welcome to my Project</title></head>
> <body>
> <form id="form1" runat="server"><div>
> <h3>Below are the files in my Project</h3>
> </div></form>
> </body></html>
>
> <script runat="server">
> protected void Page_Load(object sender, EventArgs e) {
>
> DirectoryInfo dir = new DirectoryInfo(this.MapPath("~"));
> foreach (FileInfo f in dir.GetFiles("*.as*x"))
> if (f.Name.ToLower() != "default.aspx") Response.Write("<a href='./" +
> f.Name + "'>" + f.Name + "</a>" + "<br>");
> }
> </script>
>
>
On Mar 19, 4:07 pm, Peter Bromberg [C# MVP]
<pbromb...@.yahoo.yabbadabbadoo.com> wrote:
> Instead of doing raw Response.Write's to the output stream, try databindin
g
> your "stuff" to a control defined on the Page that is suitable for renderi
ng
> such a list - a Repeater, DataList, or Gridview.
> Peter
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> Short urls & more: http://ittyurl.net
> "dotNET learner" wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
I would also suggest using code behind files instead of including the
C# logic in the ASPX page itself. This separates the code from the
design which is always a good direction to go.
Following your suggestion, I used simple Label control instead of
"Response.Write". It worked. Thank you :) The modified code is below:
protected void Page_Load(object sender, EventArgs e) {
string fileList = "";
DirectoryInfo dir = new DirectoryInfo(this.MapPath("~"));
foreach (FileInfo f in dir.GetFiles("*.as*x"))
if (f.Name.ToLower() != "default.aspx") {
fileList = fileList + "<a href='./" + f.Name + "'>" + f.Name + "</a>" +
"<br>";
}
lblFileList.Text = fileList;
}
"Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.com> wrote in
message news:5435FFC5-B40B-4AEB-8773-7E5C377AE71F@.microsoft.com...
> Instead of doing raw Response.Write's to the output stream, try
> databinding
> your "stuff" to a control defined on the Page that is suitable for
> rendering
> such a list - a Repeater, DataList, or Gridview.
> Peter
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> Short urls & more: http://ittyurl.net
>
>
> "dotNET learner" wrote:
>

No comments:

Post a Comment