我正在使用ObjectDataSource将对象绑定到GridView。在OnRowDataBound事件处理程序中,我正在尝试确定某个按钮是否应该可见。当运行时遇到此语句时,它会触发“找不到类型'Pledge'的默认成员。”错误:
lbDel.Visible = Not (e.Row.DataItem("BillingReady"))
绑定到GridView的我的Object类:
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#,但我不认为这是问题所在。如果我理解VB默认属性的C#等价物被称为索引器。这不应该有资格作为索引器吗?
public Pledge this[int index]
{
get { return ((Pledge)(List[index])); }
set { List[index] = value; }
}
答案 0 :(得分:5)
尝试将DataItem
转换为Pledge
:
Dim pledge = DirectCast(e.Row.DataItem, Pledge)
lbDel.Visible = Not pledge.BillingReady
答案 1 :(得分: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>
这是一个令人困惑的问题,所以希望答案可以帮助你节省我花时间计算出来的时间。