views:

24

answers:

1

Let's say I have a GridViewEx class declared which extends GridView. And inside that class, I have a delegate declared called GetDataPage. So it look like this:

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
}

This works ok and does what I want. But what I would like to be able to do is in the markup for the GridViewEx, be able to set this delegate, like so:

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

However, I get this error:

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

I guess it's not possible to set it via markup, but I just wondered. It's easy enough to set the delegate in code, but I was just trying to learn something new. Thanks for any help.

A: 

It sounds like what you really want to do is expose an event. Add:

public event GetDataPageDelegate GettingDataPage

Then in your markup you'll be able to say:

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

By "raising" the event in your DataBind method as such:

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

However, I would follow the pattern of events and create a new object:

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

and change your delegate to

public delegate void GettingDataPageEventHandler(object sender, GettingDataPageEventArgs);
matt-dot-net