Solved

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

Posted on 2009-05-14
6
462 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

22 Experts available now in Live!

Get 1:1 Help Now