?
Solved

making a placeholder show multiple lines...

Posted on 2007-08-08
1
Medium Priority
?
441 Views
Last Modified: 2013-11-05
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);
       
0
Comment
Question by:mrperfect75000
1 Comment
 
LVL 21

Accepted Solution

by:
Yurich earned 2000 total points
ID: 19658919
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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question