Hi,
I want to generate an anchor tag
<a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
in code behind using c#...but dunno how to do....
Hi,
I want to generate an anchor tag
<a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
in code behind using c#...but dunno how to do....
If you want the exact markup you showed, build the anchor and image as separate controls and nest the image inside the anchor. That keeps things flexible and answers ’s follow‑up about associating an image.
// Build <a> with a nested <img> in code-behind
var link = new System.Web.UI.HtmlControls.HtmlAnchor {
HRef = "javascript:void(0)" // avoid jumping to top
};
link.Attributes["class"] = "close";
link.Attributes["aria-label"] = "Close this notification";
var img = new System.Web.UI.HtmlControls.HtmlImage {
Src = ResolveUrl("~/resources/images/icons/cross_grey_small.png"),
Alt = "close"
};
img.Attributes["title"] = "Close this notification";
// Nest the image inside the anchor
link.Controls.Add(img);
// Optional: wire up a client-side click (see @pritaeas)
// Note: 'return false;' prevents navigation
link.Attributes.Add("onclick", "closeNotification(); return false;");
// Add to any container on the page (e.g., a PlaceHolder or Panel)
CloseContainer.Controls.Add(link); Notes and tips:
Attributes["class"] for the CSS class on HtmlAnchor (there is no CssClass property on the HTML control). Building on ’s approach, this is the key piece needed to mirror your original markup with an image inside.LinkButton (renders as an <a> and posts back) and style it as an icon via CSS, or place an Image next to it. For purely UI actions like closing a notice, keep it client-side and stop navigation as shown.<button type="button"> is often more accessible; if you keep the <a>, providing aria-label and meaningful alt text helps screen readers.Jump to Post— Renukavani 0Add the namespace for HtmlAnchor (using System.Web.UI.HtmlControls;),
protected void Page_Load(object sender, EventArgs e) { HtmlAnchor htmlanchor = new HtmlAnchor(); htmlanchor.HRef = "#"; htmlanchor.Title = "Welcome"; //tooltip htmlanchor.InnerText = "Welcome"; this.form1.Controls.Add(htmlanchor); }
Add the namespace for HtmlAnchor (using System.Web.UI.HtmlControls;),
protected void Page_Load(object sender, EventArgs e)
{
HtmlAnchor htmlanchor = new HtmlAnchor();
htmlanchor.HRef = "#";
htmlanchor.Title = "Welcome"; //tooltip
htmlanchor.InnerText = "Welcome";
this.form1.Controls.Add(htmlanchor);
} Thanks Renu
And how do you associate it with an image?
how to add Onclick in ancher tag in html
(OR)
Dynamically add the Ancher Tag in asp.net means codebehind to add anchor tag in asp.net
how to add Onclick in ancher tag in html
htmlanchor.Attributes["onClick"] = "myOnClick();";
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.