locked
Problem changing syntax from GridView to Repeater RRS feed

  • Question

  • User1216627406 posted

    Back for more...

    Hopefully, this is simple.

    I have the following GridView loop:

            foreach (GridViewRow row in Gridview1.Rows)
            {
                string registerNumber = (row.FindControl("txtNum") as TextBox).Text;
                if (row.RowIndex == 0)
                {
                    this.SetInitialRow2(registerNumber);
                }
    
                else
                {
                    AddNewRow2(registerNumber);
                }
                foreach (RepeaterItem Item in Repeater2.Items)
                 {
                      (Item.FindControl("BtnAdd2") as Button).Visible = Repeater2.Items.Count > 1;
                      (Item.FindControl("btnDelete") as Button).Visible = Repeater2.Items.Count > 1;
                 }
                }
    

    This works good.

    However, I am trying to use same logic but in Repeater but I am stuck on this this line:

    if (Items == 0
    ...
    

    How do I write the following line in Rpeater?

    if (row.RowIndex == 0)
    

    Here is the entire code:

            foreach (RepeaterItem Items in Repeater1.Items)
            {
                string registerNumber = (Items.FindControl("txtNum") as TextBox).Text;
                if (Items == 0)
                {
                    this.SetInitialRow2(registerNumber);
                }
    
                else
                {
                    AddNewRow2(registerNumber);
                }
                foreach (RepeaterItem Item in Repeater2.Items)
                 {
                      (Item.FindControl("BtnAdd2") as Button).Visible = Repeater2.Items.Count > 1;
                      (Item.FindControl("btnDelete") as Button).Visible = Repeater2.Items.Count > 1;
                 }
                }
    

    I have tried this:

             if (registerNumber == "0")

    No errors but it is not producing any results. It was working in GridView.

    I solved it.

    if (Items.ItemIndex == 0)

    As always, thank you

    Sunday, August 13, 2017 5:10 PM

All replies

  • User2103319870 posted

    owever, I am trying to use same logic but in Repeater but I am stuck on this this line:

    if (Items == 0
    ...
    

    How do I write the following line in Rpeater?

    I guess you want to check if Repeater control is having any rows in it. if so then you can use below code

                    foreach (RepeaterItem Items in Repeater1.Items)
                    {
                        string registerNumber = (Items.FindControl("txtNum") as TextBox).Text;
                        //check if repeater control has any rows in it
                        if (Repeater1.Items.Count < 1)
                        {
                            this.SetInitialRow2(registerNumber);
                        }
    
                        else
                        {
                            AddNewRow2(registerNumber);
                        }
                        foreach (RepeaterItem Item in Repeater2.Items)
                        {
                            (Item.FindControl("BtnAdd2") as Button).Visible = Repeater2.Items.Count > 1;
                            (Item.FindControl("btnDelete") as Button).Visible = Repeater2.Items.Count > 1;
                        }
                    }

    Monday, August 14, 2017 12:32 AM