【问题标题】:Gridview - Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound controlGridview - Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用
【发布时间】:2014-05-24 16:46:40
【问题描述】:

问题出在:

双 dItemPremium = (Eval("Premium")!= DBNull.Value) ? (double)(Eval("Premium")) : 0;

我需要帮助来改变它。

我正在使用 Gridview。

protected void dgCustomer_DataBound(object sender, System.EventArgs e)
    {
            Label pLabel = new Label();
            double dItemPremium = (Eval("Premium")!= DBNull.Value) ? (double)(Eval("Premium")) : 0;
            pLabel.Text = dItemPremium.ToString("0.00");
            dTotal_m += dItemPremium;

        dgCustomer.HeaderRow.TableSection = TableRowSection.TableHeader;
        dgCustomer.FooterRow.TableSection = TableRowSection.TableFooter;
        dgCustomer.FooterRow.Controls.Add(pLabel);

    }


    protected void dgCustomer_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 

        dTotal_m += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Premium"));

        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Totals:";
            e.Row.Cells[1].Text = dTotal_m.ToString("0.00");

            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;
        }
    }

在我使用的 HTML 中

 <asp:TemplateField>
                        <ItemTemplate>
                            <%#DataBinder.Eval(DataItem,"Premium")%></ItemTemplate>
                        <HeaderTemplate>
                            Premium Amount Paid</HeaderTemplate>
                    </asp:TemplateField>

我添加了 DataBinder 和 DataItem 但仍然没有。

谢谢!

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您可以使用控件访问代码中的绑定值。 Eval、Bind 等方法用于将值绑定到控件。有很多方法可以在代码绑定中访问绑定值,其中一种是

    e.Row.Cells[0].Text
    

    另一个是将绑定值放入 Label 并像访问它一样,

    string tempstr=((Label)dgCustomer.Rows[e.RowIndex].Cells[<cell_number>].FindControl("Label_ID")).Text;
    

    【讨论】:

    • @user3508633 我为你感到高兴。
    【解决方案2】:

    如果有人遇到这个问题,这里有另一个解决方案:将 aspx 文件从

    <ItemTemplate>
       <%= DataBinder.Eval(DataItem,"Premium")%></ItemTemplate>
    <HeaderTemplate>
    

    【讨论】:

      猜你喜欢
      • 2018-06-21
      • 2012-12-10
      • 2011-02-04
      • 2012-05-02
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      相关资源
      最近更新 更多