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

Discussion in 'ASP .Net' started by =?Utf-8?B?QWJlbCBDaGFu?=, Apr 28, 2006.

  1. 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>");

    }
    }
     
    =?Utf-8?B?QWJlbCBDaGFu?=, Apr 28, 2006
    #1
    1. Advertising

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

    "Abel Chan" <> wrote in message
    news:...
    > 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>");
    >
    > }
    > }
     
    Mark Fitzpatrick, Apr 28, 2006
    #2
    1. Advertising

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

    MyPanel.Text = sMyCustomFileListString;

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net



    "Abel Chan" <> wrote in message
    news:...
    > 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>");
    >
    > }
    > }
     
    Steve C. Orr [MVP, MCSD], Apr 28, 2006
    #3
  4. 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='<%# Eval("FullName") %>' Text='<%# Eval("Name") %>'
    ></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.)
     
    Steven Cheng[MSFT], Apr 28, 2006
    #4
  5. RE: How to use Response.write to write to a specific area on a asp

    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[MSFT]" wrote:

    > 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='<%# Eval("FullName") %>' Text='<%# Eval("Name") %>'
    > ></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.)
    >
    >
    >
    >
     
    =?Utf-8?B?QWJlbCBDaGFu?=, Apr 28, 2006
    #5
  6. RE: How to use Response.write to write to a specific area on a asp

    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.)
     
    Steven Cheng[MSFT], Apr 30, 2006
    #6
  7. =?Utf-8?B?QWJlbCBDaGFu?=

    Rahul Guest

    Re: How to use Response.write to write to a specific area on a asp

    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



    Steven Cheng[MSFT] wrote:
    > 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.)
     
    Rahul, May 3, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS News \(MS ILM\)
    Replies:
    4
    Views:
    350
    MS News \(MS ILM\)
    Feb 9, 2004
  2. Mark
    Replies:
    1
    Views:
    606
    Larry Barowski
    Jun 27, 2005
  3. hotadvice
    Replies:
    14
    Views:
    708
    hotadvice
    Oct 2, 2007
  4. Andy B.
    Replies:
    0
    Views:
    811
    Andy B.
    Apr 30, 2010
  5. MumHa

    Response.Write inside Editable Area

    MumHa, Dec 8, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    141
    MumHa
    Dec 8, 2005
Loading...

Share This Page