This is a migrated thread and some comments may be shown as answers.

Inherit GridBoundColumn : export to Excel value is empty

0 Answers 10 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexandre
Top achievements
Rank 1
Alexandre asked on 19 Feb 2019, 10:31 AM

Hello,

I've created a new GridColumn that inherits the Gridboudcolumn.

This new column contains a HtmlGenericControl (containing the text) and an image with some javascript on the client click event. Everything works fine excepts when I want to export the datas in Excel. The value is always empty.

As a workaround, I put an image with 0px height and 0px with as the first control of the cell and set the AlternateText property of this image, that does the trick but this is not a valid solution for us.

How can I do this in the good way ?

Best regards

Alexandre Stoppani

 

This is the code of my class:

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Telerik.Web.UI;

namespace Ch.Ne.Ceg.Library.Web.WebControls.Sfwk.SFWKGrid
{
    public class MailReadOnlyColumn : GridBoundColumn
    {
        #region Events

        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem)
        {
            if (inItem is GridDataItem item)
            {
                if (cell != null && cell.Controls.Count > 0)
                {
                    cell.Controls.Clear();
                }

                if (cell != null)
                    cell.Text = "";

                var alienSpan = new HtmlGenericControl();

                var nobr = new HtmlGenericControl("nobr");
                nobr.Attributes["class"] = "MailReadOnlyColumnNoBold";

                var imageHiddenForExport = new ImageButton
                {
                    Visible = true,
                    Height = new Unit(0),
                    Width = new Unit(0),
                    CommandName = "RowClick",
                    CommandArgument = item.ItemIndex.ToString(),
                    ImageAlign = ImageAlign.Middle,
                    ImageUrl = cell?.Page.ClientScript.GetWebResourceUrl(GetType(), "Ch.Ne.Ceg.Library.Web.WebControls.Images.milieu.png")
                };

                var image = new ImageButton
                {
                    Visible = true,
                    Height = new Unit(0),
                    Width = new Unit(0),
                    CommandName = "RowClick",
                    CommandArgument = item.ItemIndex.ToString(),
                    ImageAlign = ImageAlign.Middle
                };
                image.Style.Add(HtmlTextWriterStyle.PaddingLeft, "6px");
                image.Style.Add(HtmlTextWriterStyle.Height, "9px");
                image.Style.Add(HtmlTextWriterStyle.Width, "14px");
                image.Style.Add(HtmlTextWriterStyle.PaddingTop, "8px");
                image.Style.Add(HtmlTextWriterStyle.Cursor, "default");

                nobr.Controls.Add(alienSpan);
                nobr.Controls.Add(image);

                if (cell != null)
                {
                    cell.DataBinding += cell_DataBinding;

                    cell.Controls.Add(imageHiddenForExport);

                    cell.Controls.Add(nobr);
                }
            }
            else
            {
                base.InitializeCell(cell, columnIndex, inItem);
            }
        }

        void cell_DataBinding(object sender, EventArgs e)
        {
            if (!(sender is TableCell cell))
                return;

            var parent = cell.NamingContainer as GridDataItem;

            var imageHiddenForExport = cell.Controls[0] as ImageButton;
            var lbl = cell.Controls[1].Controls[0] as HtmlGenericControl;
            var imgButton = cell.Controls[1].Controls[1] as ImageButton;

            if (parent == null)
                return;

            var t = parent.DataItem.GetType();
            var p = t.GetProperty(DataField);

            if (p == null)
                return;

            var value = (string)p.GetValue(parent.DataItem, null);

            if (lbl != null)
                lbl.InnerText = string.IsNullOrEmpty(value) ? "-" : value;

            if (imageHiddenForExport != null)
                imageHiddenForExport.AlternateText = value;
            
            if (imgButton != null)
            {
                if (string.IsNullOrEmpty(value))
                {
                    imgButton.ImageUrl = null;
                    imgButton.Enabled = false;
                    imgButton.Visible = false;
                }
                else
                {
                    imgButton.ImageUrl = ((TableCell)sender).Page.ClientScript.GetWebResourceUrl(GetType(), "Ch.Ne.Ceg.Library.Web.WebControls.Images.enveloppe.png");
                    imgButton.AlternateText = value;
                    imgButton.OnClientClick = "mywindow=window.open('mailto:" + value + "'); if(mywindow != null) mywindow.close(); return false;";
                    imgButton.Visible = true;
                    imgButton.Enabled = true;
                }
            }
        }

        #endregion
    }
}

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Alexandre
Top achievements
Rank 1
Share this question
or