Search code examples
asp.netmethodscode-behind

Call code-behind method from .aspx site


i want to call a method from a code-behind file. I already read all threads/posts etc. but it don't work!

I tried to

  • call Image1.DataBind(); Image1.ImageUrl="test.jpg"
  • call OnDataBinding="methodname" and the correct method
  • call ImageUrl='<%# GetImage(1) %> or <%= GetImage(1) %>

Here is some code snippet from my files.

ASPX Site:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Abstimmung.aspx.cs" Inherits="Schulsprecher.Abstimmung" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
.
.
<form id="form1" runat="server">
.
.
<asp:Image ID="Image1" runat="server" Width="250px" Height="300px" ImageUrl='<%# GetImage(1) %>' />

Code-Behind File

protected string GetImage(string index)
{
    return "test.jpg";
}

Solution

  • If you can change it to normal html image tag, you can do this

    <img ID="Image2" width="250" height="300" src='<%= GetImage("1") %>' />
    

    Is it mandatory you should use <asp:Image />? I tried that with no success