Nick Kahn
Nick Kahn

Reputation: 20078

creating hyperlink dynamically

i am trying to create a hyperlink from code-behind, but it is not creating it where i want it to be.

if i look at my souce code is creating somewhere else and from .aspx page it seems like everything is in place where it needs to be.

.aspx

<div class="AZ">
    <ol class="AZlist">   
            <asp:PlaceHolder runat="server" ID="AToZ"></asp:PlaceHolder>  
    </ol>
</div>

.codebehind

HyperLink links = new HyperLink();
links.Text =  "<li>" + CheckMe.ToString() + "</li>";
links.NavigateUrl = "<a href='#'" + CheckMe.ToString() + ">";
ph.Controls.Add(links); 

source code:

.... .... ...

   <div class="AZ">
    <ol class="AZlist">
           // I WANT HYPERLINK HERE....!!!!!!!!!!!
    </ol>
    <br />
</div>
 </li>

but its creating outside the div area here

<a href="#A"><li>A</li></a>

Upvotes: 0

Views: 2271

Answers (3)

iandayman
iandayman

Reputation: 4467

Why not use an asp:repeater instead.

You could then put all your Mark up in the aspx template making use of header / detail / footer templating. Next just get the text and URL values into a bindable format in the code behind and data bind.

Saves doing markup in code behind and the need to dynamically insert controls which can cause headaches on post backs.

Upvotes: 0

Trevor
Trevor

Reputation: 4860

Ditch the way you're doing it here. Try making your ol tag runat="server" and give it an id. Then create a new ListItem control, add the hyperlink control, and add the ListItem to the ol.

Something like this in the codebehind:

dim anLi = new ListItem([can't remember exact parameters])
dim aHyperlink = new Hyperlink([whatever to initialize])
anLi.addControl(aHyperlink)
theOl.controls.add(anLi)

Upvotes: 0

BBQ
BBQ

Reputation: 618

I dont think you should put those tags in the .text and .navigateurl properties. just put the link and the text in them. Put the <li> tags around the placeholder.

Upvotes: 1

Related Questions