Search code examples
c#asp.neteventslabelrepeater

Executing code when ASP.NET site is completely done rendering


I've looked around and tried to find a solution but haven't been able to find one I could adapt to my issue.

Basically I got a repeater pulling paths to images out of a SQL database, with it there are 3 value defining height, width, and price, this is shown with 3 labels under the image. However I don't want the label to show if the values are 0, so I wrote this foreach to sort it:

foreach (RepeaterItem Rep1 in Repeater1.Items)
{
        //assigns a temporary variable the value of the control we find.
        Label nullPris = (Label)Rep1.FindControl("PrisLabel");
        Label nullHeight = (Label)Rep1.FindControl("HeightLabel");
        Label nullWidth = (Label)Rep1.FindControl("WidthLabel");

        //Checks if the lable has the text we're looking for.
        if (nullPris.Text == "0,00 Kr.-" || nullPris.Text == "0.00 Kr.-")
        {
            //If it has, stop rendering the label.
            nullPris.Visible = false;
        }

        if (nullHeight.Text == "Højde: 0,00 cm" || nullHeight.Text == "Højde: 0.00 cm")
        {
            //If it has, stop rendering the label.
            nullHeight.Visible = false;
        }

        //Checks if the lable has the text we're looking for.
        if (nullWidth.Text == " Bredde: 0,00 cm" || nullWidth.Text == "Bredde: 0.00 cm")
        {
            //If it has, toggle the visibility..
            nullWidth.Visible = false;
        }
}

The code itself works as intended, however I've run into 2 issues:

  • If I call the code in any of the Page_Events it doesn't work, the code runs but doesn't find the repeater so skips the rest entirely, it dosen't matter if I put it in Page_Load or Page_Unload, both seems to be executed before the repeater has done its job.

  • Secondly I got around this by calling the code via OnLoad on the labels themselves, but then the last loaded item isn't affected, this happens regardless of the amount of items in the database.

Thanks in advance!


Solution

  • I found a solution to my problem that didn't require me to wait for the page to load.

    I found other people with a similar problem using a For each loop on items created by a repeater, so I took out the loop and instead executed the code each time a new item was bound to the repeater using the OnItemDataBound method, so my revised code looks like this:

    protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    
        {
            //assigns a temporary variable the value of the control we find.
            Label nullPris = (Label)e.Item.FindControl("PrisLabel");
            Label nullHeight = (Label)e.Item.FindControl("HeightLabel");
            Label nullWidth = (Label)e.Item.FindControl("WidthLabel");
    
            //Checks if the lable has the text we're looking for.
            if (nullPris.Text == "0,00 Kr.-" || nullPris.Text == "0.00 Kr.-")
            {
                //If it has, stop rendering the label.
                nullPris.Visible = false;
            }
    
            if (nullHeight.Text == "Højde: 0,00 cm" || nullHeight.Text == "Højde: 0.00 cm")
            {
                //If it has, stop rendering the label.
                nullHeight.Visible = false;
            }
    
            //Checks if the lable has the text we're looking for.
            if (nullWidth.Text == " Bredde: 0,00 cm" || nullWidth.Text == "Bredde: 0.00 cm")
            {
                //If it has, toggle the visibility..
                nullWidth.Visible = false;
            }
        }
    }