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

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

问题描述

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

解决方案

为了完成你想要的,你可以看到两个选项: p>

1。)在代码中动态构建每个TemplateField,并根据一些配置进行切换。

2.)为您的自定义网格创建用户控件并使用



我知道你说你不想使用UserControl,因为这会消除你动态改变你的布局的能力,但让我挑战这个预设以一个例子。



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

 < asp:PlaceHolder ID =GridViewPlaceHolderrunat =server/> 

您的自定义网格可以声明地在.ascx文件中构建,然后在运行时如此加载到位:

  GridViewPlaceHolder.Controls.Add(LoadControl(〜/ Controls / MyCustomControl.ascx)); 

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

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

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

 < asp:GridView ID =myGridViewrunat =serverAutoGenerateColumns =false> 
<列>
< 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>

您的代码背后的控件将如下所示:

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

现在只使用这个页面或控件投给基类,你可以一般使用它。以下是一个简单的例子,说明如何使用这个,但是我认为这很清楚:

  protected void Page_Load(对象发件人,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);
}

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


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.) 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.

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.

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" />

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