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 个问题:
如果我调用任何 Page_Events
中的代码它不起作用,代码 运行s 但没有找到转发器所以完全跳过其余部分,我把它放在 Page_Load
或 Page_Unload
中都没关系,两者似乎都在中继器完成其工作之前执行。
其次,我通过标签本身上的 OnLoad
调用代码来解决这个问题,但是最后加载的项目不受影响,无论项目数量如何,都会发生这种情况数据库。
提前致谢!
如果可能,请尝试查看 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;
}
}
}
我环顾四周并试图找到解决方案,但未能找到适合我的问题的解决方案。
基本上我有一个中继器从 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 个问题:
如果我调用任何
Page_Events
中的代码它不起作用,代码 运行s 但没有找到转发器所以完全跳过其余部分,我把它放在Page_Load
或Page_Unload
中都没关系,两者似乎都在中继器完成其工作之前执行。其次,我通过标签本身上的
OnLoad
调用代码来解决这个问题,但是最后加载的项目不受影响,无论项目数量如何,都会发生这种情况数据库。
提前致谢!
如果可能,请尝试查看 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;
}
}
}