2012-04-04 39 views
1

我正在使用ObjectDataSource将对象绑定到GridView。在OnRowDataBound事件处理程序中,我试图确定某个按钮是否应该可见或不可见。当运行时遇到这个语句时,它会触发一个“找不到'Pledge'类型的默认成员。”错误:没有找到类型'MyType'的默认成员

lbDel.Visible = Not (e.Row.DataItem("BillingReady")) 

我Object类绑定到GridView:

public class Pledges : System.Collections.CollectionBase 
{ 
    public Pledge this[int index] 
    { 
     get { return ((Pledge)(List[index])); } 
     set { List[index] = value; } 
    } 

    public int Add(Pledge pledge) 
    { 
     return List.Add(pledge); 
    } 
} 

我的誓言类:

public class Pledge 
{ 
    public int PledgeID { get; set; } 
    public int EventID { get; set; } 
    public int SponsorID { get; set; } 
    public int StudentID { get; set; } 
    public decimal Amount { get; set; } 
    public string Type { get; set; } 
    public bool IsPaid { get; set; } 
    public string EventName { get; set; } 
    public DateTime EventDate { get; set; } 
    public bool BillingReady { get; set; } 
    public string SponsorName { get; set; } 
    public int Grade_level { get; set; } 
    public string StudentName { get; set; } 
    public string NickName { get; set; } 
    public int Laps { get; set; } 
    public decimal PledgeSubtotal { get; set; } 
} 

我OnRowDataBound事件处理程序:

Protected Sub PledgeGrid_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    If (e.Row.RowType = DataControlRowType.DataRow) And _ 
     Not ((e.Row.RowState = DataControlRowState.Edit) Or ((e.Row.RowState = DataControlRowState.Alternate) And (e.Row.RowState = DataControlRowState.Edit))) Then 
     Dim lbDel As LinkButton 
     Dim lbEd As LinkButton 
     lbDel = CType(e.Row.FindControl("lbDelete"), LinkButton) 
     lbEd = CType(e.Row.FindControl("lbEdit"), LinkButton) 

     If ((e.Row.RowState = DataControlRowState.Normal) Or (e.Row.RowState = DataControlRowState.Alternate)) Then 
      lbDel.Visible = Not (e.Row.DataItem("BillingReady")) '<-- Problem happens here 
      lbEd.Visible = Not (e.Row.DataItem("BillingReady")) 
     End If 
    End If 
End Sub 

是,我不得不混合VB和C#b我不认为这是问题。如果我明白VB默认属性的C#等价物被称为索引器。这不应该成为索引器吗?

public Pledge this[int index] 
{ 
    get { return ((Pledge)(List[index])); } 
    set { List[index] = value; } 
} 
+0

可能重复的[ASP.NET Repeater错误:找不到类型为xx的默认成员](http://stackoverflow.com/questions/9673445/asp-net-repeater-error-no-default-member-found- for-type-xx) – 2016-03-24 16:47:20

回答

5

尝试施放DataItemPledge

Dim pledge = DirectCast(e.Row.DataItem, Pledge) 
lbDel.Visible = Not pledge.BillingReady 
+0

这样做!谢谢。 – BKahuna 2012-04-05 02:11:25

1

我结合一个C#模式,以VB DataGrid中时有同样的问题。

通常在aspx文件时,DataGrid能与模板列显示的字段:

 <asp:templatecolumn headertext="Reference"> 
      <itemtemplate> 
       <%# Container.DataItem("Reference")%> 
      </itemtemplate> 
     </asp:templatecolumn> 

但在使用我的C#模型时,将DataGrid需要的模板列语法:

 <asp:templatecolumn headertext="Reference"> 
      <itemtemplate> 
       <%# DataBinder.Eval(Container.DataItem, "Reference") %> 
      </itemtemplate> 
     </asp:templatecolumn> 

这是一个令人困惑的问题,所以希望答案能帮助您节省我花费的时间。

相关问题