如何从后面的代码动态更新图像URL

时间:2017-05-10 14:15:33

标签: c# asp.net image webforms code-behind

我有这个页面:

<form id="form1" runat="server">
   <asp:Image id="imgLogo" runat="server" />

   <div class="content">
       <asp:ScriptManager ID="scrptMan" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="updChooser" runat="server">
            <ContentTemplate>
                <h3>Company name</h3>
                <asp:DropDownList ID="ddCompany" runat="server" DataTextField="CompanyName" DataValueField="Id" AutoPostBack="true" OnSelectedIndexChanged="ddCompany_OnSelectedIndexChanged"></asp:DropDownList>
                <h3>Year</h3>
                <asp:DropDownList ID="ddYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddYear_OnSelectedIndexChanged"></asp:DropDownList>
                <asp:Literal ID="ltAdvice" runat="server"></asp:Literal>
                <strong><asp:Literal ID="ltYear" runat="server"></asp:Literal></strong>
                <asp:Panel ID="pnlNext" runat="server" Visible="false">
                    <div class="submit">
                        <asp:Button ID="btnNext" runat="server" Text="Next" CssClass="button" OnClick="btnNext_OnClick"/>
                    </div>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</form>

如果用户选择公司,我想动态更新代码隐藏的徽标图片,如此

    private void FillLogo(int companyId)
    {
        //temporary

        byte[] logo = File.ReadAllBytes(@"D:\temp\images\baseLogo.png");

        if (clientId > 0 && File.Exists(@"D:\temp\images\companies\" + companyId + ".png"))
        {
            logo = File.ReadAllBytes(@"D:\temp\images\companies\" + companyId+ ".png");
        }

        imgLogo.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(logo);
    }

我在ddCompany_OnSelectedIndexChanged方法中调用此方法,并在OnPreRender方法中使用正确的公司ID,但是第一次加载徽标并且ddCompany中的任何更改都不会更新。 有可能实施吗?

0 个答案:

没有答案