【问题标题】:How do I format dynamically added footer columns of GridView如何格式化动态添加的 GridView 页脚列
【发布时间】:2023-03-28 16:35:01
【问题描述】:

我创建了下面的代码,使用来自 ASP 的 GridView。网格几乎按照我想要的方式工作。新控件会自动将列添加到 gridview 的页脚,并在需要时添加“保存”、“取消”和“添加”按钮。只有一件事我无法正常工作。当网格为空时,将添加带有列和按钮的页脚,但它不是水平布局,而是垂直创建字段。原因是这些字段没有包含在“td”元素中。字段和按钮都可以工作,但布局错误。关于如何解决这个问题的任何线索?

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Fortrus.Intranet.WebControls
{
    /// <summary>
    /// Produces a GridView with 'Edit', 'Cancel' and 'Insert' buttons
    /// </summary>
    public class CttGridView : GridView
    {
        public CttGridView()
        {
            ShowHeaderWhenEmpty = true;
            ShowFooter = true;
            RowDataBound += CttGridView_RowDataBound;
        }

        void CttGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[e.Row.Cells.Count - 1].Width = new Unit(50, UnitType.Pixel);
        }

        /// <summary>
        /// This class is used to fill the different templates in the gridview dynamically
        /// </summary>
        public class StandardGridViewTemplateGenerator : ITemplate
        {

            private ListItemType Type;
            private DataControlFieldCollection Columns;

            /// <summary>
            /// In this variable the initialy defined columns are saved
            /// This is needed because we also add a column to the gridview
            /// </summary>
            private DataControlFieldCollection InitialColumns;

            public StandardGridViewTemplateGenerator(ListItemType type, DataControlFieldCollection columns = null)
            {
                Type = type;
                Columns = columns;

                if (columns != null)
                {
                    InitialColumns = new DataControlFieldCollection();

                    // Save the columns initialy defined for the gridview
                    foreach (DataControlField column in columns)
                    {
                        InitialColumns.Add(column);
                    }
                }
            }

            /// <summary>
            /// Method of the ITemplate interface is called when a specific template is needed
            /// </summary>
            void ITemplate.InstantiateIn(Control container)
            {
                switch (Type)
                {
                    // The tamplate for editing is needed
                    case ListItemType.EditItem:
                        CreateEditButtons(container);
                        break;

                    // A display template is needed
                    case ListItemType.Item:
                        CreateItemButtons(container);
                        break;

                    // A footer template is needed
                    case ListItemType.Footer:
                        CreateColumnControls(container);
                        CreateEmptyButtons(container);
                        break;

                    case ListItemType.SelectedItem:
                    case ListItemType.AlternatingItem:
                    case ListItemType.Header:
                    case ListItemType.Pager:
                    case ListItemType.Separator:
                    default:
                        throw new NotImplementedException();
                }
            }
            /// <summary>
            /// Adds buttons for the empty (footer) template
            /// </summary>
            private void CreateEmptyButtons(Control container)
            {
                AddButton(container,
                    id: "AddNewButton",
                    cssClass: "CttGridViewSaveButton",
                    commandName: "Insert",
                    toolTip: "Toevoegen");
                AddButton(container,
                    id: "CancelNewButton",
                    cssClass: "CttGridViewCancelButton",
                    commandName: "Cancel",
                    toolTip: "Ongedaan maken");
            }

            /// <summary>
            /// Generate the footer controls from the initial columns
            /// </summary>
            private void CreateColumnControls(Control container)
            {
                if (InitialColumns != null)
                {
                    foreach (TemplateField column in InitialColumns)
                    {
                        column.EditItemTemplate.InstantiateIn(container);
                    }
                }
            }

            /// <summary>
            /// Adds buttons for the Edit template
            /// </summary>
            private void CreateEditButtons(Control container)
            {
                AddButton(container,
                    id: "SaveButton",
                    cssClass: "CttGridViewSaveButton",
                    commandName: "Update",
                    toolTip: "Opslaan");
                AddButton(container,
                    id: "CancelButton",
                    cssClass: "CttGridViewCancelButton",
                    commandName: "Cancel",
                    toolTip: "Ongedaan maken");
            }

            /// <summary>
            /// Adds buttons for the (display) item template
            /// </summary>
            private void CreateItemButtons(Control container)
            {
                AddButton(container,
                    id: "EditButton",
                    cssClass: "CttGridViewEditButton",
                    commandName: "Edit",
                    toolTip: "Bewerken");
                AddButton(container,
                    id: "DeleteButton",
                    cssClass: "CttGridViewDeleteButton",
                    commandName: "Delete",
                    toolTip: "Verwijderen",
                    confirmation: "return confirm('Weet u zeker dat u deze rij wilt verwijderen?');");
            }

            /// <summary>
            /// Adds a button to the container
            /// </summary>
            private static void AddButton(Control container, string id, string cssClass, string commandName, string toolTip, string confirmation = null)
            {
                ImageButton button = new ImageButton();

                button.ID = id;
                button.CssClass = cssClass;
                button.CommandName = commandName;
                button.ToolTip = toolTip;
                button.OnClientClick = confirmation;
                // Added blank image to prevent the default gray placeholder border
                // Image should be set using the background-image from CSS
                button.ImageUrl = "data:image/png;base64,R0lGODlhFAAUAIAAAP///wAAACH5BAEAAAAALAAAAAAUABQAAAIRhI+py+0Po5y02ouz3rz7rxUAOw==";

                container.Controls.Add(button);
            }
        }

        /// <summary>
        /// Create the child controls
        /// </summary>
        protected override void CreateChildControls()
        {
            ShowFooter = true;

            InitializeTemplate();

            base.CreateChildControls();
        }

        /// <summary>
        /// Initialize the templates
        /// </summary>
        private void InitializeTemplate()
        {
            TemplateField template = new TemplateField();

            template.ItemTemplate = new StandardGridViewTemplateGenerator(ListItemType.Item);
            template.EditItemTemplate = new StandardGridViewTemplateGenerator(ListItemType.EditItem);
            template.FooterTemplate = new StandardGridViewTemplateGenerator(ListItemType.Footer);

            EmptyDataTemplate = new StandardGridViewTemplateGenerator(ListItemType.Footer, columns: Columns);

            Columns.Add(template);
        }
    }
}

【问题讨论】:

    标签: c# html asp.net gridview


    【解决方案1】:

    我解决了。这是完整的代码:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Fortrus.Intranet.WebControls
    {
        /// <summary>
        /// Produces a GridView with 'Edit', 'Cancel' and 'Insert' buttons
        /// </summary>
        public class CttGridView : GridView
        {
            public CttGridView()
            {
                ShowHeaderWhenEmpty = true;
                ShowFooter = true;
                RowDataBound += CttGridView_RowDataBound;
            }
    
            void CttGridView_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Width = new Unit(50, UnitType.Pixel);
            }
    
            /// <summary>
            /// This class is used to fill the different templates in the gridview dynamically
            /// </summary>
            public class StandardGridViewTemplateGenerator : ITemplate
            {
    
                private ListItemType Type;
                private DataControlFieldCollection Columns;
    
                /// <summary>
                /// In this variable the initialy defined columns are saved
                /// This is needed because we also add a column to the gridview
                /// </summary>
                private DataControlFieldCollection InitialColumns;
    
                public StandardGridViewTemplateGenerator(ListItemType type, DataControlFieldCollection columns = null)
                {
                    Type = type;
                    Columns = columns;
    
                    if (columns != null)
                    {
                        InitialColumns = new DataControlFieldCollection();
    
                        // Save the columns initialy defined for the gridview
                        foreach (DataControlField column in columns)
                        {
                            InitialColumns.Add(column);
                        }
                    }
                }
    
                /// <summary>
                /// Method of the ITemplate interface is called when a specific template is needed
                /// </summary>
                void ITemplate.InstantiateIn(Control container)
                {
                    switch (Type)
                    {
                        // The tamplate for editing is needed
                        case ListItemType.EditItem:
                            CreateEditButtons(container);
                            break;
    
                        // A display template is needed
                        case ListItemType.Item:
                            CreateItemButtons(container);
                            break;
    
                        // A footer template is needed
                        case ListItemType.Footer:
                            CreateColumnControls(container);
                            break;
    
                        case ListItemType.SelectedItem:
                        case ListItemType.AlternatingItem:
                        case ListItemType.Header:
                        case ListItemType.Pager:
                        case ListItemType.Separator:
                        default:
                            throw new NotImplementedException();
                    }
                }
    
                /// <summary>
                /// Generate the footer controls from the initial columns
                /// </summary>
                private void CreateColumnControls(Control container)
                {
                    if (InitialColumns != null)
                    {
                        TableRow row = new TableRow();
    
                        foreach (TemplateField column in InitialColumns)
                        {
                            TableCell cell = new TableCell();
                            row.Controls.Add(cell);
                            column.EditItemTemplate.InstantiateIn(cell);
                        }
    
                        CreateInsertButtons(row);
    
                        container.Controls.Add(row);
                    }
                    else
                    {
                        CreateInsertButtons(container);
                    }
                }
    
                /// <summary>
                /// Adds buttons for the empty (footer) template
                /// </summary>
                private void CreateInsertButtons(Control container)
                {
                    Control control = container;
    
                    if (container is TableRow)
                    {
                        control = new TableCell();
                        container.Controls.Add(control);
                    }
    
                    AddButton(control,
                        id: "AddNewButton",
                        cssClass: "CttGridViewSaveButton",
                        commandName: "Insert",
                        toolTip: "Toevoegen");
    
                    AddButton(control,
                        id: "CancelNewButton",
                        cssClass: "CttGridViewCancelButton",
                        commandName: "Cancel",
                        toolTip: "Ongedaan maken");
                }
    
                /// <summary>
                /// Adds buttons for the Edit template
                /// </summary>
                private void CreateEditButtons(Control container)
                {
                    AddButton(container,
                        id: "SaveButton",
                        cssClass: "CttGridViewSaveButton",
                        commandName: "Update",
                        toolTip: "Opslaan");
                    AddButton(container,
                        id: "CancelButton",
                        cssClass: "CttGridViewCancelButton",
                        commandName: "Cancel",
                        toolTip: "Ongedaan maken");
                }
    
                /// <summary>
                /// Adds buttons for the (display) item template
                /// </summary>
                private void CreateItemButtons(Control container)
                {
                    AddButton(container,
                        id: "EditButton",
                        cssClass: "CttGridViewEditButton",
                        commandName: "Edit",
                        toolTip: "Bewerken");
                    AddButton(container,
                        id: "DeleteButton",
                        cssClass: "CttGridViewDeleteButton",
                        commandName: "Delete",
                        toolTip: "Verwijderen",
                        confirmation: "return confirm('Weet u zeker dat u deze rij wilt verwijderen?');");
                }
    
                /// <summary>
                /// Adds a button to the container
                /// </summary>
                private static void AddButton(Control container, string id, string cssClass, string commandName, string toolTip, string confirmation = null)
                {
                    ImageButton button = new ImageButton();
    
                    button.ID = id;
                    button.CssClass = cssClass;
                    button.CommandName = commandName;
                    button.ToolTip = toolTip;
                    button.OnClientClick = confirmation;
                    // Added blank image to prevent the default gray placeholder border
                    // Image should be set using the background-image from CSS
                    button.ImageUrl = "data:image/png;base64,R0lGODlhFAAUAIAAAP///wAAACH5BAEAAAAALAAAAAAUABQAAAIRhI+py+0Po5y02ouz3rz7rxUAOw==";
    
                    container.Controls.Add(button);
                }
            }
    
            /// <summary>
            /// Create the child controls
            /// </summary>
            protected override void CreateChildControls()
            {
                ShowFooter = true;
    
                InitializeTemplate();
    
                base.CreateChildControls();
            }
    
            /// <summary>
            /// Initialize the templates
            /// </summary>
            private void InitializeTemplate()
            {
                TemplateField template = new TemplateField();
    
                template.ItemTemplate = new StandardGridViewTemplateGenerator(ListItemType.Item);
                template.EditItemTemplate = new StandardGridViewTemplateGenerator(ListItemType.EditItem);
                template.FooterTemplate = new StandardGridViewTemplateGenerator(ListItemType.Footer);
    
                EmptyDataTemplate = new StandardGridViewTemplateGenerator(ListItemType.Footer, columns: Columns);
    
                Columns.Add(template);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多