How to use Response.write to write to a specific area on a aspx pa

G

Guest

Hi there,

I am trying to use ASP.NET to populate list of files with hyper link from a
specific directory. The following code works well but it populates the list
to the top of the web page. I have header and footer defined on the web
page. How could I write the list to the body session like a specific area, a
table or a list box control?

Thanks in advance.

private void Page_Load(object sender, System.EventArgs e)
{

DirectoryInfo dir = new DirectoryInfo("C:\\temp\\Test");

if (!dir.Exists)
{
throw new DirectoryNotFoundException("The directory does not exist.");
}

// Call the GetFileSystemInfos method.
FileSystemInfo[] infos = dir.GetFileSystemInfos();

foreach (FileSystemInfo i in infos)
{
Response.Write("<A HREF='");
Response.Write (i.Name);
Response.Write ("'>");
Response.Write (i.Name);
Response.Write ("</A><br>");

}
}
 
M

Mark Fitzpatrick

This would work fine in classic ASP, but you need to convert it into the
object oriented ASP.Net.

You can use an asp literal control in the page as a placeholder for the
content you want to use.

<asp:Literal id="literalList" runat="server" />

Now, instead of Response.Writing them directly to the browser, we're going
to create a string that we will then pass to the literal control.

String contatenation is an expensive operation so the .Net framework has the
StringBuilder class available in the System.Text namespace.

StringBuilder sb = new StringBuilder();
foreach (FileSystemInfo i in infos)
{
sb.AppendFormat("<a href=\"{0}\">{0}</a>",i.Name);
}

literalList.Text = sb.ToString();


Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
S

Steve C. Orr [MVP, MCSD]

Put a Panel or PlaceHolder control on the page.
Then use code something like this:

MyPanel.Text = sMyCustomFileListString;
 
S

Steven Cheng[MSFT]

Hi Abel,

As other members mentioned, you can use a LiteralControl to hold the
htmltext or use a Panel control as the container and the Literal or Panel
control can be put at a specified position on the page.

BTW, as for the generation of the File Info List, have you considered
modified it to use a more ASP.NET oriented way? I have a suggestion on
using the ASP.NET template databound control such as Repeater or DataList
control. We can define the control template (for the list) in the databound
template and then use databinding to populate the list. For example, we can
use the following Repeater to display the File Info list:

===========aspx==============

<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br /><asp:HyperLink ID="linkFile"
runat="server"
NavigateUrl=' said:
</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>


</div>
</form>
</body>
================================

============code behind===========
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DirectoryInfo di = new
DirectoryInfo(Server.MapPath("~/Common/"));
FileInfo[] pages = di.GetFiles("*.aspx");

Repeater1.DataSource = pages;
Repeater1.DataBind();
}
}
===========================

Then, you can add the repeater control at the desired position on the
webform page.

The above code and template is specific to ASP.NET 2.0. If you're using
asp.net 1.x , or if there is anything unclear here, please feel free to
post here.

Here is the msdn reference introducing the repeater control:

#Repeater Web Server Control Overview
http://msdn2.microsoft.com/en-us/library/x8f2zez5.aspx

Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

You guys are great!!! I got all these replies within 6 hours!!!

I have tried Mark and Steve's suggestion and they both work.

I have ASP.net 1.x. I will take a look at the new template feature. I will
migrate to ASP.net 2.0 later this year.

Thanks again for all your help. ASP.NET newsgroup rocks!!!

Abel

Steven Cheng said:
Hi Abel,

As other members mentioned, you can use a LiteralControl to hold the
htmltext or use a Panel control as the container and the Literal or Panel
control can be put at a specified position on the page.

BTW, as for the generation of the File Info List, have you considered
modified it to use a more ASP.NET oriented way? I have a suggestion on
using the ASP.NET template databound control such as Repeater or DataList
control. We can define the control template (for the list) in the databound
template and then use databinding to populate the list. For example, we can
use the following Repeater to display the File Info list:

===========aspx==============

<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br /><asp:HyperLink ID="linkFile"
runat="server"
NavigateUrl=' said:
</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>


</div>
</form>
</body>
================================

============code behind===========
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DirectoryInfo di = new
DirectoryInfo(Server.MapPath("~/Common/"));
FileInfo[] pages = di.GetFiles("*.aspx");

Repeater1.DataSource = pages;
Repeater1.DataBind();
}
}
===========================

Then, you can add the repeater control at the desired position on the
webform page.

The above code and template is specific to ASP.NET 2.0. If you're using
asp.net 1.x , or if there is anything unclear here, please feel free to
post here.

Here is the msdn reference introducing the repeater control:

#Repeater Web Server Control Overview
http://msdn2.microsoft.com/en-us/library/x8f2zez5.aspx

Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Cool Abel,

Glad that those suggestion are of assistance. :)

Please always feel free to post here when there is anything we can help you.

Have a good day!

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

Rahul

Hi Guys

Something silly is happening or may be I am too tired.

I have been doing asp.net from last 4 years. In asp.net 2.0 I am just
setting the NavigateUrl property of the Hyperlink to something Dynamic
and
I do not see the Hyperlink rendered. There is no underline (Linkable
hyperLink")

<asp:HyperLink ID="hlAdd" runat="server" NavigateUrl='<%#
"~/Default.aspx?" + "T=0"%>' Text='<%$ Resources:CoreWebFM,Lbl_Add
%>'></asp:HyperLink>



What I see is the word "Add" coming from my resource file but with no
Link.

Any Ideas , What basic thing I am doing wrong.

Thanks
Rahul
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top