Access Button Icon from DataBoundItem?

1 Answer 138 Views
Button Grid
David
Top achievements
Rank 1
David asked on 02 Aug 2021, 10:57 PM

I'd like to have a RadButton whose icon differs between two images depending on the value of the field. I'm not sure if this is the best approach, but I am defaulting to one specific icon and attempting to change it in the _ItemDataBound method. However, I can't seem to figure out how to access the icon from the RadButton:

                        <ItemTemplate>
                            <telerik:RadButton RenderMode="Lightweight" ID="Image2"
                                CommandName="OnCommentToggleHandled" runat="server">
                                <Icon PrimaryIconUrl='<%# DataBinder.Eval(Container.DataItem, "Dismissed").Equals(true) ? "/App/images/green-check.png" : "/App/images/comment.gif" %>' runat="server"></Icon>
                            </telerik:RadButton>
                        </ItemTemplate>
        protected void StatusGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;
                RadButton foo = dataBoundItem["Dismissed"].Controls[1] as RadButton;

                if ((dataBoundItem["Important"].Controls[1] as Image).ImageUrl.Contains("green-check.png"))
                {
                    dataBoundItem.BackColor = System.Drawing.Color.LemonChiffon;
                }
                else if ((dataBoundItem["Dismissed"].Controls[1] as RadButton).PrimaryIconUrl.Contains("blank.gif"))
                {
                    e.Item.CssClass = "rgRow unhandledMessage ";
                }

                dataBoundItem["Text"].Text = WebUtility.HtmlDecode(dataBoundItem["Text"].Text);
            }
        }
I tried looking through the documentation, but it's very unclear -- I didn't even realize that <Icon> had to be a child within the button until stumbling upon a third-party example, and even then I couldn't figure out how to query to the icon from the C# code. Can someone please show me how to reach the icon in the C# code, or possibly show me another way to do what I am trying to do?

1 Answer, 1 is accepted

Sort by
1
Accepted
Doncho
Telerik team
answered on 05 Aug 2021, 11:53 AM

Hi David,

Please note that the Telerik.Web.UI.RadButtonIcon object does not have a DataBinding event, hence you cannot use binding expressions in it.

Instead you can try setting the icon directly in the RadButton tag:

<telerik:RadButton RenderMode="Lightweight" ID="RadButton1"
    CommandName="OnCommentToggleHandled" runat="server"
    Icon-PrimaryIconUrl='<%# DataBinder.Eval(Container.DataItem, "Dismissed").Equals(true) ? "/App/images/green-check.png" : "/App/images/comment.gif" %>'>
</telerik:RadButton>
And in the code-behind, you can access the PrimaryIconUrl as shown below:

RadButton foo = dataBoundItem["Dismissed"].Controls[1] as RadButton;
var iconUrl = foo.Icon.PrimaryIconUrl;

Please let me know if any further questions come up.

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

David
Top achievements
Rank 1
commented on 06 Aug 2021, 02:58 AM

Thanks, the foo.Icon.PrimaryIconUrl method worked perfectly.

I'm very new to this framework and code base and it would be nice if documentation and examples of these two methods were present in the docs.

Doncho
Telerik team
commented on 10 Aug 2021, 11:08 AM

Thank you for the feedback, David!

Indeed this information is present in the Configure RadButton With Icons section in our documentation. Still, we will consider making changes in the documentation so that the needed is easier for finding in the future.

Tags
Button Grid
Asked by
David
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or