【问题标题】:GridView delegate question in ASP.NetASP.Net 中的 GridView 委托问题
【发布时间】:2010-07-02 16:21:24
【问题描述】:

假设我声明了一个扩展GridViewGridViewEx 类。在该类中,我声明了一个名为GetDataPage 的委托。所以它看起来像这样:

public class GridViewEx : GridView
{
    public delegate object GetDataPageDelegate(int pageIndex, int pageSize, string sortExpression,
        IList<FilterItem> filterItems);

    [Browsable(true), Category("NewDynamic")]
    [Description("Method used to fetch the data for this grid")]
    public GetDataPageDelegate GetDataPage
    {
        get
        {
            return ViewState["pgv_getgriddata"] as GetDataPageDelegate;
        }
        set
        {
            ViewState["pgv_getgriddata"] = value;
        }
    }

    // ... other parts of class omitted
}

这可以正常工作并且可以满足我的要求。但我想做的是在 GridViewEx 的标记中,能够设置这个委托,如下所示:

<div style="margin-top: 20px;">
    <custom:GridViewEx ID="gridView" runat="server" SkinID="GridViewEx" Width="40%" AllowSorting="true"
        VirtualItemCount="-1" AllowPaging="true" GetDataPage="Helper.GetDataPage">
    </custom:GridViewEx>
</div>

但是,我收到此错误:

Error 1 Cannot create an object of type 'GUI.Controls.GridViewEx+GetDataPageDelegate' from its string representation 'Helper.GetDataPage' for the 'GetDataPage' property.

我想不可能通过标记来设置它,但我只是想知道。在代码中设置委托很容易,但我只是想学习一些新东西。感谢您的帮助。

【问题讨论】:

  • 只是一个问题,但是您为委托提供的对象是否需要其构造函数中的参数?也许该区域周围的某些东西导致了您的问题。
  • 我认为构造函数不会影响它。我认为可能无法使用标记设置委托,但我想我会在这里问一下以确保:)。
  • 首先,如果您在标记中定义视图状态的值,那么使用视图状态毫无意义。如果您正在动态设置值,例如从数据源绑定它们,则使用 Viewstate。你在哪里调用 GetDataPage?
  • @Joroen - 关于视图状态的要点。我从 GridViewEx 的 DataBind 方法中调用 GetDataPage。

标签: asp.net gridview delegates


【解决方案1】:

听起来你真正想做的是公开一个事件。添加:

public event GetDataPageDelegate GettingDataPage

然后在你的标记中你可以说:

<custom:GridViewEx ID="gridView" runat="server" SkinID="GridViewEx" Width="40%" AllowSorting="true" 
    VirtualItemCount="-1" AllowPaging="true" OnGettingDataPage="Helper.GetDataPage"> 
</custom:GridViewEx>

通过在您的 DataBind 方法中“引发”事件,如下所示:

if(GettingDataPage!=null)
   GettingDataPage(pageIndex,pageSize,sortExpression,filterItems);

但是,我会按照事件的模式创建一个新对象:

public class GettingDataPageEventArgs : EventArgs
{
   public int PageIndex{get;set;}
   public int PageSize{get;set;}
   public string SortExpression{get;set;}
   public IList<FilterItem> FilterList{get;set;}
}

并将您的代表更改为

public delegate void GettingDataPageEventHandler(object sender, GettingDataPageEventArgs);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多