Solved

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

Posted on 2009-05-14
6
467 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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

762 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