locked
How to call repaterone eval in repatertwo RRS feed

  • Question

  • User2033107836 posted

    Hello

    In Repeater1 i have this code

    Text='<%# Eval("Qty") %>'

    I want to call this in Repeater2 how to do so..? in asp.net 2.0 C#

    Tuesday, July 18, 2017 12:57 PM

Answers

  • User-335504541 posted

    Hi asp.ambur,

    Are these two repeaters nested?

    If so, you could try to use Repeater2's OnItemDataBound event to get the value in Repeater1 then set to the control.

    For example:

    In aspx:

                  <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="OnItemDataBound">
                    <HeaderTemplate>
                        <table class="Grid" border="1">
                            <tr>
                                <th scope="col">&nbsp;
                                </th>
                                <th scope="col" style="width: 150px">Qty1
                                </th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                                    <HeaderTemplate>
                                        <table class="ChildGrid" border="1">
                                            <tr>
                                                <th scope="col" style="width: 150px">Qty2</th>
                                            </tr>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <tr>
                                            <td>
                                                <asp:Label ID="Qty2" runat="server"  />
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        </table>
                                    </FooterTemplate>
                                </asp:Repeater>
                            </td>
                            <td>
                                <asp:Label ID="Qty" runat="server" Text='<%# Eval("Qty") %>'/>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    In behind code:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    Repeater1.DataSource = GetData();
                    Repeater1.DataBind();
                }
            }
    
            private static DataTable GetData()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.Add("Qty");
                dt.Rows.Add(1);
                dt.Rows.Add(2);
                dt.Rows.Add(3);
                return dt;
    
            }
    
            protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater Repeater2 = e.Item.FindControl("Repeater2") as Repeater;
                    Repeater2.DataSource = GetData();
                    Repeater2.DataBind();
                }
            }
    
            protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
    
                    Label label = e.Item.FindControl("Qty2") as Label;
                    label.Text=  ((sender as Repeater).Parent.FindControl("Qty") as Label).Text;
                }
            }

    If not, you could try to add "Qty" to Repeater2's datasource.

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 19, 2017 3:01 AM

All replies

  • User-335504541 posted

    Hi asp.ambur,

    Are these two repeaters nested?

    If so, you could try to use Repeater2's OnItemDataBound event to get the value in Repeater1 then set to the control.

    For example:

    In aspx:

                  <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="OnItemDataBound">
                    <HeaderTemplate>
                        <table class="Grid" border="1">
                            <tr>
                                <th scope="col">&nbsp;
                                </th>
                                <th scope="col" style="width: 150px">Qty1
                                </th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                                    <HeaderTemplate>
                                        <table class="ChildGrid" border="1">
                                            <tr>
                                                <th scope="col" style="width: 150px">Qty2</th>
                                            </tr>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <tr>
                                            <td>
                                                <asp:Label ID="Qty2" runat="server"  />
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        </table>
                                    </FooterTemplate>
                                </asp:Repeater>
                            </td>
                            <td>
                                <asp:Label ID="Qty" runat="server" Text='<%# Eval("Qty") %>'/>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    In behind code:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    Repeater1.DataSource = GetData();
                    Repeater1.DataBind();
                }
            }
    
            private static DataTable GetData()
            {
    
                DataTable dt = new DataTable();
                dt.Columns.Add("Qty");
                dt.Rows.Add(1);
                dt.Rows.Add(2);
                dt.Rows.Add(3);
                return dt;
    
            }
    
            protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater Repeater2 = e.Item.FindControl("Repeater2") as Repeater;
                    Repeater2.DataSource = GetData();
                    Repeater2.DataBind();
                }
            }
    
            protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
    
                    Label label = e.Item.FindControl("Qty2") as Label;
                    label.Text=  ((sender as Repeater).Parent.FindControl("Qty") as Label).Text;
                }
            }

    If not, you could try to add "Qty" to Repeater2's datasource.

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 19, 2017 3:01 AM
  • User2033107836 posted

    Hello Bily Liu

    How to call this in eval Qty2

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
    
                    Label label = e.Item.FindControl("Qty2") as Label;
                    label.Text=  ((sender as Repeater).Parent.FindControl("Qty") as Label).Text;
                }
    Wednesday, July 19, 2017 6:20 AM
  • User-335504541 posted

    Hi asp.ambur,

    asp.ambur

    How to call this in eval Qty2

    This code is called in Repeater2's OnItemDataBound event.

    The event will get the value of Qty in Repeater1 and set the value to the control you want.

    In the example, the value is set to the label which id is "Qty2".

    And you don't need to use 

    Text='<%# Eval("Qty") %>'

    in Repeater2.

    Best Regards,

    Billy

    Wednesday, July 19, 2017 7:25 AM