【问题标题】:Selecting an Item or column in a Repeater and changing the Data在中继器中选择项目或列并更改数据
【发布时间】:2014-06-13 11:44:28
【问题描述】:

我正在尝试更改中继器中的数据 到目前为止,我正在按 Item 和 AlternatingItem 循环遍历每一行 但我想更改最后一列中的值 使该值精确到小数点后两位。我有一个基本的 连接到填充转发器的存储过程 代码如下:

连接到存储过程:

    SqlDataAdapter da = new SqlDataAdapter("Stored Procedure", conn);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    da.SelectCommand.Parameters.Add(new SqlParameter("@Admin", "ALL"));
    DataSet dataset = new DataSet();
    da.Fill(dataset);
    rptItems.DataSource = dataset.Tables[0];
    rptItems.DataBind();

ItemDataBound 方法的代码:

    protected void rptconsole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType  ==ListItemType.AlternatingItem)
        {
           //Code should go here which changes the values of the last column
           //To two decimal places
        }
    }

用于填充转发器的 html/XAML 代码:

<asp:Repeater ID="rptconsole" runat="server" OnItemDataBound="rptconsole_ItemDataBound">

       <HeaderTemplate>

            <table id="tablework">

                <th>Console</th>
                <th>Color</th>
                <th>Features</th>
                <th>Description</th>
                <th>price</th>

</HeaderTemplate>
        <ItemTemplate>

            <tr>
                <td align="center"><%# Eval("[ConsoleType]") %></td>
                <td align="center"><%# Eval("[color]") %></td>
                <td align="center"><%# Eval("[features]") %></td>
                <td align="center"><%# Eval("[desc]") %></td>
                <td align="center"><%# Eval("[price]") %></td>
            </tr>


        </ItemTemplate>
        <AlternatingItemTemplate>

            <tr>
        <td align="center"><%# Eval("[ConsoleType]") %></td>
                <td align="center"><%# Eval("[color]") %></td>
                <td align="center"><%# Eval("[features]") %></td>
                <td align="center"><%# Eval("[desc]") %></td>
                <td align="center"><%# Eval("[price]") %></td>
            </tr>
        </AlternatingItemTemplate>

        <FooterTemplate>
            </table>
        </FooterTemplate> 
    </asp:Repeater>

【问题讨论】:

  • 是否要对“价格”列的值进行四舍五入?
  • 其实我现在想把它保留到小数点后两位,比如 12.445665764433
  • 我发布了一个答案,只是更新它,请检查
  • 我在下面收到一个错误 ("0.00"),它说... 参数类型字符串不可分配给参数类型 System.IFormatProvider
  • 我更新了答案,请检查增益。使用这个Convert.ToDecimal(tdPrice.InnerText).ToString("C2");

标签: c# asp.net xaml visual-studio-2012


【解决方案1】:

像我一样更改 HTML,我将 td 设置为运行服务器

   <HeaderTemplate>

        <table id="tablework">

            <th>Console</th>
            <th>Color</th>
            <th>Features</th>
            <th>Description</th>
            <th>price</th>

        <tr>
            <td align="center"><%# Eval("[ConsoleType]") %></td>
            <td align="center"><%# Eval("[color]") %></td>
            <td align="center"><%# Eval("[features]") %></td>
            <td align="center"><%# Eval("[desc]") %></td>
            <td  runat="server" id="price" align="center"><%# Eval("[price]") %></td>
        </tr>


    </ItemTemplate>
    <AlternatingItemTemplate>

        <tr>
    <td align="center"><%# Eval("[ConsoleType]") %></td>
            <td align="center"><%# Eval("[color]") %></td>
            <td align="center"><%# Eval("[features]") %></td>
            <td align="center"><%# Eval("[desc]") %></td>
            <td  runat="server" id="price" align="center"><%# Eval("[price]") %></td>
        </tr>
    </AlternatingItemTemplate>

    <FooterTemplate>
        </table>
    </FooterTemplate> 
</asp:Repeater>

之后,您需要在服务器端找到该 td 并需要像我在下面所做的那样更改值

protected void rptconsole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType  ==ListItemType.AlternatingItem)
        {
           HtmlTableCell tdPrice = e.Item.FindControl("price") as HtmlTableCell;    
           tdPrice.InnerText = Convert.ToDecimal(tdPrice.InnerText).ToString("C2"); //2dp Number or any value you want
        }
    }

参考:Formatting a float to 2 decimal places

【讨论】:

  • 感谢您的响应,但 tdPrice.InnerText=.... 我不想通过硬代码更改它 我想将该列中已经存在的数字更改为两位小数
【解决方案2】:

您根本不需要为此使用 OnItemDataBound 事件。

您可以在 ASP 标记中这样做:

...
<td  runat="server" id="price" align="center"><%# String.Format("{0:F2}", Eval("[price]")) %></td>
...

或者如果您想使用 .NET 货币格式:

...
<td  runat="server" id="price" align="center"><%# String.Format("{0:C}", Eval("[price]")) %></td>
...

查看这篇文章以获取有关 .NET 标准数字格式字符串的更多具体信息:http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多