【问题标题】:Add text from c# code to a gridview label将 c# 代码中的文本添加到 gridview 标签
【发布时间】:2012-12-01 20:56:35
【问题描述】:

我需要在 gridview 上的 itemtemplate 中添加特定文本...

现在我的网格视图中有这个

<asp:TemplateField HeaderText="Total" SortExpression="Total" ItemStyle-Width="100px">
    <ItemTemplate>
        <asp:Label ID="lblTotal" runat="server" Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'>
        </asp:Label>
    </ItemTemplate>
</asp:TemplateField>

在它说的部分

<asp:Label ID="lblTotal" runat="server" Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'>

我做了一个特定的文本,但它总是相同的文本(当然除了在 Eval 中)...但是我需要从这个方法中获取我需要的格式。

public static string GetFormatoMoneda(decimal decCantidad)
{
    //Get data from currency (Dollars, Pesos, Euros, etc.)
    DataRow dr = ConexionBD.GetInstanciaConexionBD().GetTipoDeMonedaPrincipal((int)HttpContext.Current.Session["Grupo"]);

    return dr["Signo"] + Math.Round(decCantidad, 2).ToString("C").Substring(1) + " " + dr["Abreviatura"];
}

我使用这种方法来获取特定的字符串并在标签上使用它(我在 cs 文件的代码上分配它).. 但在这种情况下...我必须在 gridview 的列上插入该文本...

如何获取该字符串值并将其插入到模板字段/项目模板内的标签上??

【问题讨论】:

  • Text='' 有效吗?

标签: c# asp.net gridview label itemtemplate


【解决方案1】:

而不是...

Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'

...使用

Text='<%#GetFormatoMoneda(Eval("Total"))%>'

但是,这假定 GetFormatoMoneda 与 Web 表单属于同一类。如果没有,则需要包含类名,例如

Text='<%#MyClass.GetFormatoMoneda(Eval("Total"))%>'

然后您需要更改 GetFormatoMoneda 以使用对象类型参数,例如

public static string GetFormatoMoneda(object objCantidad)
{
    var decCantidad = Convert.ToDecimal(decCantidad);

    //Get data from currency (Dollars, Pesos, Euros, etc.)
    DataRow dr = ConexionBD.GetInstanciaConexionBD().GetTipoDeMonedaPrincipal((int)HttpContext.Current.Session["Grupo"]);

    return dr["Signo"] + Math.Round(decCantidad, 2).ToString("C").Substring(1) + " " + dr["Abreviatura"];
}

或者使用其他方法带对象参数并调用GetFormatoMoneda(decimal),传入正确的值,如

protected string CorrectFormat(object obj)
{
    return GetFormatoMoneda(Convert.ToDecimal(obj));
}

在这种情况下你会使用

Text='<%#CorrectFormat(Eval("Total"))%>'

【讨论】:

    【解决方案2】:

    如果您想以编程方式执行此操作,则可以:

    默认.aspx:

    <asp:GridView ID="gvGrid" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvGrid_RowDataBound">
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Label ID="lblTotal" runat="server" />
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
    

    默认.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        //Generate fake data
        var data = Enumerable.Range(1, 20);
    
        //Give the data to the grid
        gvGrid.DataSource = data;
        gvGrid.DataBind();
      }
    }
    
    protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        //Find the control
        var lblTotal = (Label)e.Row.FindControl("lblTotal");
    
        //Get the data for this row
        var data = (int)e.Row.DataItem;
    
        //Display the data
        lblTotal.Text = data.ToString();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多