ASP.NET 站点完全完成渲染后执行代码

Executing code when ASP.NET site is completely done rendering

我环顾四周并试图找到解决方案,但未能找到适合我的问题的解决方案。

基本上我有一个中继器从 SQL 数据库中提取图像的路径,它有 3 个值定义高度、宽度和价格,这在图像下方显示有 3 个标签。但是我不希望标签显示值是否为 0,所以我写了这个 foreach 来对其进行排序:

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

代码本身按预期工作,但是我 运行 遇到了 2 个问题:

提前致谢!

如果可能,请尝试查看 ajax calls/javascript 基本上您希望在文档准备就绪时触发一个函数...对吗?

A small description of what I'm trying to say

我找到了不需要等待页面加载的解决方案。

我发现其他人对转发器创建的项目使用 For each 循环也有类似的问题,所以我去掉了循环,而是在每次使用 OnItemDataBound 方法将新项目绑定到转发器时执行代码,所以我修改后的代码如下所示:

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