-1

I have a asp label that I need to be able to change according to the code behind. How can I do this?

ASPX: (The first part works correctly only for "TestA@abc.com" and the second part dynamically changes the label (EmailLabel) according to the "if" statement in the code behind. How can I integrate these two so the label is mailto? Thanks.

<p>Email at <a href="mailto:TestA@abc.com?subject=Comments">TestA@abc.com</a>.</p>

<p>Email at <asp:Label ID="EmailLabel" runat="server"></asp:Label>.</p>

Code Behind:

public changeLabel()
{
 if (//Some Condition Here)
 {
    this.EmailLabel.Text = "TestA@abc.com";
 }
 else
 {
  this.EmailLabel.Text = "TestB@abc.com";
 }
}
Keven Diuliy
  • 131
  • 2
  • 5
  • 12

2 Answers2

1

What you are trying to do there won't work. Label's render out as <span> tags, so it will never be "clickable". You want to do something more like this:

<p>Email at <a href="mailto:TestA@abc.com?subject=Comments">TestA@abc.com</a>.</p>

<p>Email at <asp:LinkButton ID="EmailLabel" runat="server"></asp:LinkButton>.</p>

And then instead of changing the Text property, change the NavigateUrl property.

You could also use an HtmlControl, which is basically a standard HTML tag that you add the runat="server" attribute to.

<p>Email at <a id="EmailLabel" runat="server" href=""></a>.</p>

You would then be able to modify this <a> tag via server side code, the properties will be slightly different, but now you've got a real live anchor tag to work with.

This other SO question might also be helpful: How to launching email client on LinkButton click event?

Community
  • 1
  • 1
CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
0
<html>
<body>
<span id="foo" 
      onclick="document.getElementById('foo').innerHTML = 'never say never'"
>
    Click Me!
</span>
</body>
</html>

For your Label, just remember that .Text is the server equivalent of .innerHTML so you can put whatever HTML you want right into the asp:Label when setting .Text. Just watch out for cross-site-scripting exploits.

ebyrob
  • 26
  • 2