<\/script>')

Calling code behind a method from an aspx page

I have an image tag like

<asp:Image ID="ImgProduct" runat="server"    ImageUrl='<%# FormatImageUrl("10")%>' /> 

and in the code I have a method like

protected string FormatImageUrl(string s)
{
return "image"+s;
}

when I rum the code, I expect the HTML image tag with src = "image10" to be displayed.

but nothing happens. Why? any clues?

I am at asp.net. not mvc

+5
source share
4 answers

on aspx page

<asp:Image ID="ImgProduct" runat="server" ondatabinding="ImgProduct_DataBinding" />

in cs file use this

protected void Page_Load(object sender, EventArgs e)
{
    ImgProduct.DataBind();

}
protected void ImgProduct_DataBinding(object sender, EventArgs e)
{
    ImgProduct.ImageUrl = "Image pathe + name";
}
+1
source

<%# .. %>Applies only to data binding. One solution is to call manuallyDataBind()

Try

protected void Page_Load(object sender, EventArgs e)
{
        ImgProduct.DataBind();
}
+2
source

Page.DataBind() Control.DataBind(). <% #% > .

+2

.

protected void Page_Load(object sender,EventArgs e)
{
    if(!IsPostBack)
    {
        ImgProduct.ImageUrl = FormatImageUrl("10");
    }
}

protected string FormatImageUrl(string s)
{
    return "image"+s;
}

I don’t understand what difference does it make for you to bind it to it or write code on the code behind. Saving a few keystrokes? It would be very easy to also observe objects on Codebehind, and not with a data binding expression model

+1
source

All Articles