我必须使用一种方法在里面添加一些
<ul id="testul" runat="server">
<%# LoadModalForm(testul) %>
</ul>
代码背后:
protected Control LoadModalForm(Control ulCtrl)
{
var li = new HtmlGenericControl("li");
// creating link with href
var lnk = new HyperLink { NavigateUrl = "#modal-editprofile" };
lnk.Attributes.Add("data-toggle", "modal");
lnk.Text = "Edit";
li.Controls.Add(lnk);
ulCtrl.Controls.Add(li);
return ulCtrl;
}
但我得到例外:
Exception Details: System.Web.HttpException: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
详细信息:
[HttpException (0x80004005): The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.]
System.Web.UI.ControlCollection.Add(Control child) +12283968
答案 0 :(得分:0)
您不需要函数调用:
<ul id="testul" runat="server">
</ul>
然后在像Page_Load
这样的事件中 LoadModalForm(testul);
您不需要从LoadModalForm返回任何内容,它已经添加了子控件。
答案 1 :(得分:0)
HTML:
<ul id="testul" runat="server">
</ul>
代码背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
LoadModalForm(testul);
}
protected void LoadModalForm(Control ulCtrl)
{
var li = new HtmlGenericControl("li");
// creating link with href
var lnk = new HyperLink { NavigateUrl = "#modal-editprofile" };
lnk.Attributes.Add("data-toggle", "modal");
lnk.Text = "Edit";
li.Controls.Add(lnk);
ulCtrl.Controls.Add(li);
}