【问题标题】:No default member found for type 'MyType'找不到类型“MyType”的默认成员
【发布时间】:2012-04-04 22:18:05
【问题描述】:

我正在使用 ObjectDataSource 将对象绑定到 GridView。在 OnRowDataBound 事件处理程序中,我试图确定某个按钮是否应该可见。当运行时遇到此语句时,它会触发“No default member found for type '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; }
}

【问题讨论】:

标签: c# vb.net gridview


【解决方案1】:

尝试将DataItem 转换为Pledge

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多