making a placeholder show multiple lines...

Hi,

I hjave this code that i'm using to show a link on a site - but i want to be able to show multiple links....is there anyway to do this with a placeholder?

so when i click the button it shows me the link - when i click it again - it clears the first link and put a new one - i want it to show the old one and the new one...

HyperLink hl;
             hl = new HyperLink();
             hl.Target = "blank";
             hl.Text = link;
             hl.NavigateUrl = link;
           
             PlaceHolder1.Controls.Add(hl);
       
mrperfect75000Asked:
Who is Participating?
 
YurichConnect With a Mentor Commented:
that happens because each time you press a button your page is sent back to the server and then re-rendered anew - affectively resetting anything what you did before the button was pressed (adding hyper links in your case) - that's basic mechanic of asp.net web pages.

if you create any dynamic controls, you have to create 'em each time your page is loaded. you can do it in several methods, but for now we'll stick with page load. you also will need to store your info between posts and we'll do it in the session control. Here is the sample web applications, you can copy-paste it into any new web app, see how it works, and use it in your own application. There're two parts, an aspx and a source code:

==== aspx ==============
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Place Holder.aspx.cs" Inherits="Place_Holder" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />&nbsp;</div>
    </form>
</body>
</html>

====== source code ================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections.ObjectModel;


public partial class Place_Holder : System.Web.UI.Page
{
      // collection of your hyper links
      Collection<HyperLink> hlinks = new Collection<HyperLink>();


      protected void Page_Load( object sender, EventArgs e )
      {
            // on each page load we check if there is anything in the session for our links
            // and load all previously stored links in the place holder

            if( Session[ "links" ] != null )
            {
                  hlinks = ( Collection<HyperLink> )Session[ "links" ];
                  foreach( HyperLink hlink in hlinks )
                        PlaceHolder1.Controls.Add( hlink );
            }
      }

      protected void Button1_Click( object sender, EventArgs e )
      {
            // create a new link
            HyperLink hl = new HyperLink();
            hl.NavigateUrl = @"http://google.co.nz";
            hl.Text = "google " + DateTime.Now.ToLongTimeString();
            
            // add it to the holder for now
            PlaceHolder1.Controls.Add( hl );

            // and add it to the collection for after-post back
            hlinks.Add( hl );
            Session[ "links" ] = hlinks;
      }
}

You can add some literal controls after each hyperlink to break 'em to new lines as well.

Good luck,
Yurich

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.