动态更改 GridView 项模板 [英] Dynamically change GridView item template

查看:28
本文介绍了动态更改 GridView 项模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的 asp.net 网站,它在很多地方使用绑定到同一对象的 GridView.我正在使用项目模板来自定义每一行.但是,要在所有页面中使用相同的模板,我必须复制 &将项目模板粘贴到每个页面.显然,这不是最好的解决方案.最重要的是,我希望能够通过更改一些配置文件来更改 GridView 使用的模板.一种选择是使用 DataGrid 创建用户控件并公开要在每个页面中使用的必要属性.然而,这并不能满足能够动态更改模板的第二个要求.基本上我正在寻找一种方法来告诉 GridView 使用模板并能够动态地做到这一点.任何想法都会有所帮助.

I have a fairly big asp.net website that use GridView bound to the same object in lots of places. I'm using an item template to customize each row. However to have the same template in all the pages I have to copy & paste the item template to each page. Obviously this is not the best solution. On top of this I want to be able to change the template used by the GridView, by changing some configuration file. One option would be to create an user control with the DataGrid and expose the necessary properties to be used in each page. However this does not fulfill the second requirement to be able do dynamically change the template. Basically I'm looking for a way to say to the GridView to use a template and be able to do this dynamically. Any idea would be helpful.

推荐答案

为了完成你想要的,在我看来你有两个选择:

In order to accomplish what you want, you have two options as I see it:

1.) 在代码中动态构建每个 TemplateField,并根据一些配置切换它们.
2.) 为您的自定义网格创建用户控件并使用它们.

1.) Build each TemplateField dynamically in code, and switch these based on some configuration.
2.) Create user controls for your custom grids and use those instead.

我知道你说过你不想使用 UserControl 因为这会剥夺你动态改变布局的能力,但让我用一个例子来挑战这个预设.

I know you said you don't want to use a UserControl because that will take away your ability to dynamically change your layout, but let me challenge that presupposition with an example.

您可以利用内置的 ASP.Net 功能,通过使用 PlaceHolder 控件.

You can make use of built-in ASP.Net features in order to dynamically switch out user controls to your liking by using a PlaceHolder Control.

<asp:PlaceHolder ID="GridViewPlaceHolder" runat="server" />

您的自定义网格可以在 .ascx 文件中以声明方式构建,然后在运行时动态加载到位,如下所示:

Your custom grids can be built declaratively in .ascx files and then loaded into place dynamically at runtime like so:

GridViewPlaceHolder.Controls.Add(LoadControl("~/Controls/MyCustomControl.ascx"));

现在,如果您真的想让您的生活更轻松,那么您可以创建一个抽象基类,您的所有自定义网格控件都将从该基类继承.通过这种方式,您的控件在加载时可以被一般对待.

Now, if you really want to make your life easier, then you can create an abstract base class that all your custom grid controls will inherit from. In this way, your controls can be treated generically when loaded.

public abstract class CustomGridControl: System.Web.UI.UserControl
{
    public abstract Object DataSource { get; set; }
}

可以在标记中定义一个简单的网格:

A simple grid can be defined in markup:

<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label Text='<%#Eval("Name") %>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Age">
            <ItemTemplate>
                <asp:Label Text='<%#Eval("Age") %>' runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

你的控件背后的代码看起来像这样:

And your code behind for that control would look something like this:

public partial class SimpleGrid : CustomGridControl
{
    public override object DataSource
    {
        get { return myGridView.DataSource; }
        set { myGridView.DataSource = value; }
    }
}

现在使用它的页面或控件只需将其强制转换为基类,您就可以通用地使用它.下面是一个关于如何使用它的简单示例,但我认为它清楚地说明了这一点:

Now the page or control that utilizes this only has to cast to the base class, and you can use it generically. The following is a simple example of how you might use this, but I think it makes the point clearly:

protected void Page_Load(object sender, EventArgs e)
{
    var dataSource = new List<MyCustomClass>
                        {
                            new MyCustomClass{Name = "Josh", Age = 43},
                    new MyCustomClass{Name = "Bob", Age = 14},
                    new MyCustomClass{Name = "Ashley", Age = 32},
                        };

    DynamicallyLoadUserControlGrid("~/GridViewTemplates/SimpleGrid.ascx", dataSource);
}

private void DynamicallyLoadUserControlGrid(String controlLocation, List<MyCustomClass> dataSource)
{
    var ctrl = (CustomGridControl)LoadControl(controlLocation);
    ctrl.DataSource = dataSource;
    ctrl.DataBind();

    GridViewPlaceHolder.Controls.Add(ctrl);
}

所以,你有它.自定义模板化控件没有尝试在代码中手动构建它们的所有令人讨厌的麻烦.我将在另一个答案中发布完全手动的方法,但是一旦你看到它,我想你会同意这种方法是首选.

So, there you have it. Custom templated controls without all the nasty headache of trying to build them all up manually in code. I am going to post the completely manual way of doing this in another answer, but once you see it, I think you will agree that this method is preferred.

这篇关于动态更改 GridView 项模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆