locked
How to write code for repeater row event in asp.net RRS feed

  • Question

  • User1152553138 posted

    How to write the below code for repeater control ???

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dr = (DataRowView)e.Row.DataItem;
            string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
            (e.Row.FindControl("Image1") as Image).ImageUrl = imageUrl;
        }
    }

    Friday, March 29, 2019 5:04 AM

Answers

  • User-893317190 posted

    Hi Ashraf007,

    Repeater also has  OnItemDataBound which is like OnRowDataBound,

    Below is my code.

     <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
               <HeaderTemplate>
                   <table border="1">
                       <tr> <td>name</td><td>image</td></tr>
               </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' ></asp:Label></td>
                      <td>
                          <asp:Image ID="Image1" runat="server"   />
                      </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
               </table>
                    </FooterTemplate>
            </asp:Repeater>

    cs

     protected void Page_Load(object sender, EventArgs e)
            {
                DataTable table = new DataTable();
                table.Columns.Add(new DataColumn("name"));
                table.Columns.Add(new DataColumn("image"));
                table.Rows.Add("tom", "/images/pagination_icons.png");
                table.Rows.Add("jerry", "/images/panel_tools.png");
                table.Rows.Add("pity", "/images/validatebox_warning.png");
                Repeater1.DataSource = table;
                Repeater1.DataBind();
            }
    
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
                {
                    DataRowView dr = (DataRowView)e.Item.DataItem;
                    //   string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
                    string imageUrl = dr["image"].ToString();
                    (e.Item.FindControl("Image1") as Image).ImageUrl = imageUrl;
                }
    
            }

    The result.

    Just change your code to ,   ListItemType.AlternatingItem , ListItemType.Item both represents  dataitem

     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
                {

    DataRowView dr = (DataRowView)e.Item.DataItem;
    string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["Data"]);
    (e.ITEM.FindControl("Image1") as Image).ImageUrl = imageUrl; } }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2019 8:31 AM