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='http://links.10026.com/?link=./" +
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:

Quote:

Originally Posted by

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='http://links.10026.com/?link=./" +
f.Name + "'>" + f.Name + "</a>" + "<br>");
>
}
>
</script>
>
>
>


On Mar 19, 4:07 pm, Peter Bromberg [C# MVP]
<pbromb...@.yahoo.yabbadabbadoo.comwrote:

Quote:

Originally Posted by

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:

Quote:

Originally Posted by

In the following aspx file the output from Page_Load code is rendered first
and HTML later. I want the other way round.


>

Quote:

Originally Posted by

How can I do that?


>

Quote:

Originally Posted by

<%@. Page Language="C#" %>


>

Quote:

Originally Posted by

<%@. Import Namespace="System.IO" %>


>

Quote:

Originally Posted by

<html xmlns="http://www.w3.org/1999/xhtml">


>

Quote:

Originally Posted by

<head runat="server"><title>Welcome to my Project</title></head>


>

Quote:

Originally Posted by

<body>


>

Quote:

Originally Posted by

<form id="form1" runat="server"><div>


>

Quote:

Originally Posted by

<h3>Below are the files in my Project</h3>


>

Quote:

Originally Posted by

</div></form>


>

Quote:

Originally Posted by

</body></html>


>

Quote:

Originally Posted by

<script runat="server">


>

Quote:

Originally Posted by

protected void Page_Load(object sender, EventArgs e) {


>

Quote:

Originally Posted by

DirectoryInfo dir = new DirectoryInfo(this.MapPath("~"));


>

Quote:

Originally Posted by

foreach (FileInfo f in dir.GetFiles("*.as*x"))


>

Quote:

Originally Posted by

if (f.Name.ToLower() != "default.aspx") Response.Write("<a href='http://links.10026.com/?link=./" +
f.Name + "'>" + f.Name + "</a>" + "<br>");


>

Quote:

Originally Posted by

}


>

Quote:

Originally Posted by

</script>


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='http://links.10026.com/?link=./" + f.Name + "'>" + f.Name + "</a>" +
"<br>";

}

lblFileList.Text = fileList;

}

"Peter Bromberg [C# MVP]" <pbromberg@.yahoo.yabbadabbadoo.comwrote in
message news:5435FFC5-B40B-4AEB-8773-7E5C377AE71F@.microsoft.com...

Quote:

Originally Posted by

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:
>

Quote:

Originally Posted by

>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='http://links.10026.com/?link=./" +
>f.Name + "'>" + f.Name + "</a>" + "<br>");
>>
>}
>>
></script>
>>
>>
>>

No comments:

Post a Comment