Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-14
6
Medium Priority
?
475 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 400 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 1600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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