asp:Image not displaying within an asp:HyperLink
I have an asp:Image inside an asp:HyperLink that's not being displayed. Here's the aspx.
<asp:HyperLink ID="hlSubmitSrf" runat="server" Target="_blank">
<asp:Image runat="server" ID="imgSrf" />
</asp:HyperLink>
And here's the codebehind. The Page_Init sets the hyperlink text, url, the image url, and the images's alt text.
if (srf.Count > 0)
{
actionText = "View active SRF";
hlSubmitSRF.Text = actionText;
hlSubmitSRF.NavigateUrl = "SRF_Submit.aspx?SRF_ID=" + srf[0].Srf_id.ToString();
image开发者_开发技巧Url = "images/Arrow_Right_Red.png";
}
else
{
actionText = "Submit SRF";
hlSubmitSRF.Text = actionText;
hlSubmitSRF.NavigateUrl = "SRF_Submit.aspx?APPID=" + app.Appid.ToString();
imageUrl = "images/Arrow_Right_Green.png";
}
imgSrf.ImageUrl = imageUrl;
imgSrf.AlternateText = actionText;
If I move the image outside the asp:HyperLink, the image displays, so I know the path works. If I keep it inside the asp:HyperLink, the image doesn't even show up when I view page source.
Try the following:
hlSubmitSRF.ImageUrl = imageUrl;
Set the ImageUrl on the HyperLink and don't put an <asp:Image>
within the <asp:HyperLink>
.
<asp:HyperLink ID="hlSubmitSrf" runat="server" Target="_blank"></asp:HyperLink>
if (srf.Count > 0)
{
actionText = "View active SRF";
hlSubmitSRF.Text = actionText;
hlSubmitSRF.NavigateUrl = "SRF_Submit.aspx?SRF_ID=" + srf[0].Srf_id.ToString();
hlSubmitSRF.ImageUrl = "images/Arrow_Right_Red.png";
}
else
{
actionText = "Submit SRF";
hlSubmitSRF.Text = actionText;
hlSubmitSRF.NavigateUrl = "SRF_Submit.aspx?APPID=" + app.Appid.ToString();
hlSubmitSRF.ImageUrl = "images/Arrow_Right_Green.png";
}
Have you attempted to use an image button? I don't have the code in front of me, but the fields could be adjusted based on your conditions similar to the way you are using the hyperlink wrapper.
Try not setting the hyperlink text in the page_init. Probably the .Text property replaces the image (or makes it invisible). The hyperlink also has a handy .ImageUrl property (as long as you do not use url routing).
HTH
I am not sure that you can have both text and image in the asp:HyperLink control, however the following would work, like:
HtmlImage imgSrf = new HtmlImage();
HtmlAnchor hlSubmitSRF = new HtmlAnchor();
HtmlGenericControl hlSubmitSRFText = new HtmlGenericControl("span");
if (srf.Count > 0) {
actiontext = "View active SRF";
hlSubmitSRF.HRef = "SRF_Submit.aspx?SRF_ID=" + srf(0).Srf_id.ToString();
imgSrf.Src = "images/Arrow_Right_Red.png";
} else {
actiontext = "Submit SRF";
hlSubmitSRF.HRef = "SRF_Submit.aspx?APPID=" + app.Appid.ToString();
imgSrf.Src = "images/Arrow_Right_Green.png";
}
imgSrf.Alt = actiontext;
hlSubmitSRF.Controls.Add(imgSrf);
hlSubmitSRFText.InnerHtml = actiontext;
hlSubmitSRF.Controls.Add(hlSubmitSRFText);
ParentControl.Controls.Add(hlSubmitSRF);
精彩评论