Solved

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

Posted on 2009-05-14
6
461 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now