I have many A html tags in my master web page. I would like to replace their HREF values at runtime using code. How to do that? All a tags are tagged with runat="server".
You have to iterate over all the controls in the ControlsCollection and update the Href property of all controls that are of type HtmlAnchor, like this:
private void UpdateTags(Control page)
{
foreach (Control ctrl in page.Controls)
{
if (ctrl is HtmlAnchor)
{
((HtmlAnchor)ctrl).HRef = "myNewlink";
}
else
{
if (ctrl.Controls.Count > 0)
{
UpdateTags(ctrl);
}
}
}
}
You can use HRef property of AncorTag HTML Control to change it.
like this:
<a id="anchor1" runat="server"></a>
In code
void Page_Load(object sender, EventArgs e)
{
anchor1.HRef = "http://www.microsoft.com";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With