如何添加控件到页面编程,网页载入? [英] How to add control to the page programatically in page load?

查看:191
本文介绍了如何添加控件到页面编程,网页载入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图控制从code在这样的页面加载阶段添加到页面背后:

I am trying to add controls to the page from the code behind in the page load stage like this:

foreach (FileInfo fi in dirInfo.GetFiles())
{
    HyperLink hl = new HyperLink();
    hl.ID = "Hyperlink" + i++;
    hl.Text = fi.Name;
    hl.NavigateUrl = "../downloading.aspx?file=" + fi.Name + "&user=" + userIdpar;
    Page.Controls.Add(hl);
    Page.Controls.Add(new LiteralControl("<br/>")); 
}

我正的错误是 Page.Controls.Add(HL)这里的解释是:

控件集合不可能的DataBind,初始化,加载,preRender过程中被修改或卸载阶段。

The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

我能做些什么来解决这个问题?先谢谢了。

What can I do to fix this issue? Thanks in advance.

推荐答案

创建自己的容器收集和这些添加到它,而不是直接到页面控件集合。

Create your own container collection and add these to it, instead of directly to the page controls collection.

在的.aspx:

<asp:Panel id="links" runat="server" />

在code背后(我建议使用初始化事件处理程序,而不是网页加载):

In code behind (I suggest using the Init event handler rather than page load):

foreach (FileInfo fi in dirInfo.GetFiles())
{
  HyperLink hl = new HyperLink();
  hl.ID = "Hyperlink" + i++;
  hl.Text = fi.Name;
  hl.NavigateUrl = "../downloading.aspx?file=" + fi.Name + "&user=" + userIdpar;
  links.Controls.Add(hl);
  links.Controls.Add(new LiteralControl("<br/>"));
}

这篇关于如何添加控件到页面编程,网页载入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆