2010-07-02 71 views
0

比方說,我有一個GridViewEx類宣佈擴展GridView。在該課堂內部,我有一個名爲GetDataPage的代表。所以它看起來像這樣:GridView委託問題在ASP.Net

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. 

我猜猜它不可能通過標記來設置,但我只是想知道。代碼中設置委託很容易,但我只是想學習新的東西。謝謝你的幫助。

+0

只是一個問題,但您提供的委託對象要求其構造函數的參數?也許在該地區周圍的某些事情正在造成你的問 – 2010-07-02 16:38:16

+0

我不認爲構造函數會影響它。我認爲可能只是用標記來設置委託,但我想我會問這裏只是爲了確定:)。 – dcp 2010-07-02 16:47:21

+0

首先,如果您在標記中定義它的值,那麼使用viewstate是沒有意義的。 如果您正在動態設置值,則使用Viewstate,例如將它們與數據源綁定。 你在哪裏調用GetDataPage? – Jeroen 2010-07-02 18:33:10

回答

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