Reputation: 20468
hi my dear friends :
how can i get href attrib of anchor element from code behind in asp.net? (c#)
why the below codes do n't work?
aspx :
<a runat="server" id="lightbox" href='<%# GetImageurl() %>'>
<asp:Image ID="imgInrpvEdit" runat="server" ImageUrl="~/Images/Admin/Unknown.png" />
</a>
code behind :
protected string GetImageurl()
{
return "/Images/Admin/Unknown.png";
}
note : my pages base on master & content pages + In those content pages i have multiview & upper Anchor is inside a view in content page...
best regards
Upvotes: 1
Views: 6592
Reputation: 13086
You have to remove
runat="server"
and use this syntax
<%= GetImageurl() %>
If you can't remove runat="server" you can do it code-side:
protected void Page_Load(object sender, EventArgs e)
{
lightbox.Attributes.Add("href", GetImageurl());
}
Update
If you want to use your actual syntax I think you have to call DataBind method:
protected void Page_Load(object sender, EventArgs e)
{
lightbox.DataBind();
}
Upvotes: 2
Reputation: 13561
Somewhere in your page load sequence, you want to have:
lightbox.NavigateUrl = GetImageurl());
Also, you don't need to include an asp:Image, just do this right after the above:
lightbox.ImageUrl = "http://somewhere.jpg"
Upvotes: 1