Call code behind method from aspx page

前端 未结 4 1213
一向
一向 2021-01-13 06:55

i have an image tag like

\' /> 

and in

相关标签:
4条回答
  • 2021-01-13 07:42

    in the aspx page

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

    in the 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";
    }
    
    0 讨论(0)
  • 2021-01-13 07:57

    the <%# .. %> is applied only during data binding. One solution is to manually call DataBind()

    Try

    protected void Page_Load(object sender, EventArgs e)
    {
            ImgProduct.DataBind();
    }
    
    0 讨论(0)
  • 2021-01-13 07:57

    You have to call Page.DataBind() or Control.DataBind(). Otherwise the <%# %> blocks will not be evaluated.

    0 讨论(0)
  • 2021-01-13 08:00

    Why do all the databinding stuff just try the below.

    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 databind it or write the code on code behind. Saving a few key strokes?? It would be rather very easy to also watch the Object on Codebehind rather than the Data Binding expression model

    0 讨论(0)
提交回复
热议问题