在ASP.NET网站完全完成呈现时执行代码

本文关键字:执行 代码 ASP NET 网站 | 更新日期: 2023-09-27 18:24:25

我四处寻找并试图找到一个解决方案,但一直未能找到一个可以适应我的问题的解决方案。

基本上,我得到了一个从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;
        }
}

代码本身按预期工作,但我遇到了两个问题:

  • 如果我调用任何一个Page_Events中的代码,它不起作用,代码运行但没有找到中继器,所以完全跳过其余部分,无论我把它放在Page_Load还是Page_Unload中都无关紧要,两者似乎都是在中继器完成任务之前执行的。

  • 其次,我通过对标签本身的OnLoad调用代码来解决这个问题,但最后加载的项不受影响,无论数据库中的项数量如何,都会发生这种情况。

提前感谢!

在ASP.NET网站完全完成呈现时执行代码

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

我发现其他人在中继器创建的项目上使用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;
        }
    }
}

如果可能的话,试着看看ajax调用/javascript——基本上,当文档准备好时,你想激发一个函数。。。正确的

我想说的的一个小描述