Reputation: 10561
I have a HyperLink
control with text in its Text
property.
With the following code:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Text = "Test";
link.Controls.Add(img);
When I do this, the image is rendered inside a a
tag, but the text is not rendred.
Is there a way to render both the image and the text inside the Text
property without throwing a third control in to the mix?
Upvotes: 2
Views: 118
Reputation: 1410
I feel this should work out for you.
var link = new HyperLink();
var img = new HtmlGenericControl("img");
var lbl = new Label();
img.Attributes.Add("src", "text.png");
lbl.Text = "Test";
link.Controls.Add(img);
link.Controls.Add(lbl);
this.Controls.Add(link);
Upvotes: 2
Reputation: 41520
When you put any controls into the WebControl.Controls
collection, it will ignore what you have inside Text
. So if you want to render both text and other child controls, you should add the text into Controls
:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Controls.Add(new Literal{ Text = "Test"}); // this line will add the text
link.Controls.Add(img);
Upvotes: 3
Reputation: 3769
According to the MSDN article "The HyperLink control can be displayed as text or an image." So the answer is no, I'm afraid.
Upvotes: 1