Selecting an Item or column in a Repeater and changing the Data

血红的双手。 提交于 2020-02-07 06:44:05

问题


I am trying to change the data in my repeater so far I am cycling through each row by Item and AlternatingItem but I want change the values in the last column so that the value is to two decimal places. I have a basic Connection to a stored procedure which populates the repeater code below:

connection to the stored procedure:

    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();

Code for the ItemDataBound method:

    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 code to populate the repeater:

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

回答1:


change HTML like i did, I made the td to runat server

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

After that you need to find that td at server side and need to change the value like i did below

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
        }
    }

ref: Formatting a float to 2 decimal places




回答2:


You don't need to use OnItemDataBound event for that at all.

You can do it like this in ASP markup:

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

Or if you want to use .NET currency formatting:

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

Check this article for more specific information about .NET Standard Numeric Format String: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx



来源:https://stackoverflow.com/questions/24204529/selecting-an-item-or-column-in-a-repeater-and-changing-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!