Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call code-behind method from inside html tag?

I have a C# method called imageResize which I can call from the aspx web-form page:

<%# imageResize(trustPilot,100,111) %>
// Outputs string

But if I try to call it inside an html tag there is no output?

<div class="reviews-graphic"  style="background-image: url(<%# imageResize(trustPilot,100,111) %>);"></div>
// No output

Any idea what's wrong?

like image 900
CerIs Avatar asked Aug 15 '26 18:08

CerIs


1 Answers

You can add id and runat attributes to div and add style from code behind:

<div id="divReviews" runat="server" ...>
....
</div>

server side:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var imgUrl = string.Format("url({0})", imageResize(trustPilot, 100, 111));
        divReviews.Style.Add(HtmlTextWriterStyle.BackgroundImage, imgUrl);
    }
}
like image 61
Daniel B Avatar answered Aug 19 '26 02:08

Daniel B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!