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

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

问题描述

我有使用大量的地方绑定到同一个对象GridView的一个相当大的asp.net网站。我使用的项目模板定制每一行。然而,要在我所复制和放大器的页面相同的模板;粘贴该项目模板的每一页。显然,这不是最好的解决办法。在这上面我希望能够改变和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)动态构建每个模板列在code,并且这些基础上进行一些配置进行切换。

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.

我知道你说你不想使用用户控件,因为这会带走你的动态改变布局的能力,但我认为挑战presupposition用一个例子。

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,以便动态使用<用户控制开关出自己的喜好特点href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.placeholder.aspx\">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>

和背后的控制会是这个样子的code:

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);
}

所以,你有它。自定义模板控件没有试图手动建立他们都用光code所有讨厌的头痛。我要发布的另一种回答这样做的的完全手册,但是一旦你看到它,我想你会同意这种方法preferred。

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天全站免登陆