HTML first, Page_Load later

D

dotNET learner

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?



<%@ 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>
 
G

Guest

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
 
J

Jeff T

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

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.
 
D

dotNET learner

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;

}
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top