0

我的项目目前通过后面的代码将用户控件添加到 Web 表单中,例如

For Each p In userlist
            Dim uc1 As New TimeSelector

            panel.Controls.Add(New LiteralControl("<asp:panel1 runat=" & "server" & ">"))
            panel.Controls.Add(New LiteralControl(p.ToString))
            panel.Controls.Add(New LiteralControl("<asp:Updatepanel1 runat=" & "server" & ">"))
            uc1 = DirectCast(Page.LoadControl("~/UserControls/StaffScheduling/TimeSelector.ascx"), UserControl)
            uc1.ID = "UC" & j
            panel.Controls.Add(uc1)
            panel.Controls.Add(New LiteralControl("</asp:Updatepanel1>"))
            panel.Controls.Add(New LiteralControl("</asp:panel1>"))
            panel.Controls.Add(New LiteralControl("<p></p>"))
            j += 1
        Next

除了隐藏代码之外,还有更好的方法将用户控件添加到 Web 表单吗?我是否采取了最好的方法?

问候

4

1 回答 1

0

If there is maximum no of User Controls that you need to add, why not add then in the markup?

Why you ask?

  1. People often run into problems with there usercontrols not showing.
  2. Usercontrols events not getting fired, because the usercontrols do not exist in the markup instead are dynamically generated.
  3. There is no difference in the speed(page-size). You can toggle there visibility according to your needs.
  4. Much cleaner, elegant solution.

Frankly, you should use this approach to save yourself from numerous hassles arising from your approach.

于 2012-06-28T13:59:04.253 回答