Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I created a custom link button control so it can display also an image. Code below:

namespace SharepointTools.Controls
{
    [ToolboxData("<{0}:CustomImageButton runat=\"server\"></{0}:CustomImageButton>")]
    public class CustomImageButton : LinkButton
    {

        public string ImageUrl { get; set; }
        public string ButtonId { get; set; }



        /// <summary>
        /// This image will override any background mentioned in CSS. Use this in case you want to use ASP.net (non-CSS) style URLs (like ~/images/add.png)
        /// </summary>
        public string BackgroundImageUrl { get; set; }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {

            if (!string.IsNullOrEmpty(this.ImageUrl))
            {
                base.Style.Add("background", "url('" + base.ResolveUrl(this.ImageUrl) + "') left center no-repeat");
                base.Style.Add("background-size", "32px 32px");
            }
            base.Style.Add("text-decoration", "none");
            base.Style.Add("text-align", "center");
            base.RenderBeginTag(writer);

        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            //string spanId = string.IsNullOrEmpty(this.ButtonId) ? string.Empty : "id='" + this.ButtonId;
            writer.Write("<span style='vertical-align:top;'>");
            //writer.Write("<img border='0' style='margin-top:2px;margin-left:3px' src='" + base.ResolveUrl(this.ImageUrl) + "' />");
            base.RenderContents(writer);
            writer.Write("</span>");

        }

    }

}

Below is the asp markup I made to display my custom link:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div class="lefPanel" id="leftPanel" style="float: left; width: 30%;">
                <asp:Panel ID="ListPanel" runat="server">
                    <asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDS">
                        <LayoutTemplate>
                            <div id="accordion">
                                <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                            </div>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <h3>
                                <a href="#">
                                    <%# Eval("Key")%></a></h3>
                            <asp:ListView ID="ListView2" runat="server" DataSource='<%# Eval("Items")%>'>
                                <LayoutTemplate>
                                    <div>
                                        <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                                    </div>
                                </LayoutTemplate>
                                <ItemTemplate>
                                    <div id="ElementDiv" class="elem ui-accordion ui-widget ui-helper-reset ui-accordion-header ui-helper-reset ui-state-default ui-state-active"
                                        style="vertical-align: middle">


                                        <spt:CustomImageButton ID="ImageButton1" runat="server" CommandName="SelectItem"
                                            OnCommand="ImageButton1_SelectItem" ImageUrl='<%# new SPFieldUrlValue(Eval("ApplicationType.Icon") as string).Url %>'
                                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Application.Id")%>'
                                            Text=' <%# DataBinder.Eval(Container.DataItem, "ApplicationType.ApplicationName") %>' />
                                    </div>
                                </ItemTemplate>
                            </asp:ListView>
                        </ItemTemplate>
                    </asp:ListView>
                </asp:Panel>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>

When my web part is loaded by the first time, everything works ok - buttons are rendered correctly with corresponding image. The problem is, that when I click the button and UpdatePanel reloads, image is gone. ImageUrl in the CustomImageButton is null when debugging. If I use native ImageButton from ASP, the problem does not occur. I don't know why this is happening. Text is rendered correctly which I assume is related to the fact, that my custom button extends LinkButton.

share|improve this question

1 Answer 1

The solution is quite easy - I just had to put image URL into ViewState:

public string ImageUrl 
{
   get { return (string)ViewState["ImageUrl "]; }
   set { ViewState["ImageUrl "] = value; }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.