Solved

How to add an anchor<a> dinamically to an object in C#?

Posted on 2009-05-14
6
469 Views
Last Modified: 2013-11-07
HI experts;

I have a WebUsercontrol (Num1.ascx) that contains a placeholder(MyPlaceholder).
This placeholder will contain a WebUser control(Num2.ascx) loading it up dinamically using C#.

The Num2.ascx contain 2 controls. One is a Label and the other one is a Drop Down List.
The information for this control are loaded from database(this part is done).

What I am trying to do:

I want to add dinamically an anchor <a> to Num2.ascx through the codebehind in Num1.ascx.
This is the anchor:
<a id="informationLinkPass" href="javascript:OpenDiv('Div2',
'ctl00_ctl00_ContentPlaceHolder1_MainContent_txtPassword');"><img alt="Click for Information" title="Click for Information" src="images/common/InformationIconTransparent.png"/></a>

What I have so far:

I created an object(object1) in Num1.ascx that will load  the Num2.ascx dinamically :
      Num2 object1= (Num2)Page.LoadControl(@"~/Controls/Num2.ascx");

It is in this object "object1"  where  I need to add the element anchor<a>. Later on I will add this object to my placeholder in Num1.ascx, so it can be display in the page :
 - MyPlaceholder.Controls.Add(object1);

What "object1" need to have after this operation are the following controls::
- Label
- Drop Down List
- Anchor (new control add it in dinamycally with some properties)

Could you give me an example using my code how can I add the anchor dinamically to this object?
Thank you




Num1.ascx:
        <div id="re1">
            <ul class="re2">
                <asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>
            </ul>
        </div>
 
Num2.ascx:
        <li>
             <asp:Label ID="lblQuestion" runat="server" Text="Question?"></asp:Label>    
             <p><asp:DropDownList ID="ddlAnswers" runat="server"></asp:DropDownList></p>    
        </li>

Open in new window

0
Comment
Question by:eddyperu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:MaxOvrdrv2
MaxOvrdrv2 earned 100 total points
ID: 24384967
dim anc as new HTMLAnchor

anc.href=""
anc.target=""

and so forth...
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24384980
well, maybe that won't work for you... it depends which version of .Net you're using.
0
 
LVL 4

Author Comment

by:eddyperu
ID: 24385194
HI I am using c#  net2.0:
    HtmlAnchor NewAnchorControl = new HtmlAnchor();
                    NewAnchorControl.Name = "informationLinkWork";
                    NewAnchorControl.HRef = "javascript:OpenDiv('Div2',   'ctl00_ctl00_ContentPlaceHolder1_MainContent_txtPassword');";

Am I going in the right path? How can I add <img> to an anchor?
<img alt="Click for Information" title="Click for Information" src="images/common/InformationIconTransparent.png"/>

Thanks
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 9

Expert Comment

by:tculler
ID: 24385458
I'm not a pro with HTML and ASP, but I think the following will work, though it may be a work-around to an "official" way of doing something. I suggest using the InnerText property, as shown in the following code snippet.

Also, the word you're looking for is spelled dynamically, not dinamically. www.dictionary.com, spelling is essential for professionalism :)
anchorName.InnerText = "<img src="source" alt="alt" title="title">";

Open in new window

0
 
LVL 9

Accepted Solution

by:
tculler earned 400 total points
ID: 24387143
Whoops, forgot a couple things. The code snippet should be as follows, instead
anchorName.InnerText = "<img src=\"SourcePath\" alt=\"Alternate Text\" title=\"Title\" />";

Open in new window

0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24430784
yeah just add the image to th inner text attribute.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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