日期:2014-05-18 浏览次数:20491 次
.... <myCtrl:PicUpload ID="fileUpload1" runat ="server" TargetFolder ="~/Docs/" FilePrefix ="doc" /> <br /> <asp:Button ID="btnMore" runat ="server" Text ="添加更多控件" /> <br /> <asp:Panel ID ="panelSpec" runat ="server" > </asp:Panel> ....
Shared ctrlUploadList As New List(Of UserControl_PicUpload) Shared ctrlID As Integer = 2 '目标页面中已经存在了一个同样的控件,ID为1,动态加载的控件ID从2开始 Protected Sub btnMore_Click(sender As Object, e As System.EventArgs) Handles btnMore.Click 'PicUpload为控件名称,放在UserControl文件夹内 Dim newFileUpload = New UserControl_PicUpload() With { _ .ID = "fileUpload" & ctrlID.ToString, _ .FilePrefix = "doc", _ .TargetFolder = "~/Docs/"} 'panelSpec是一个panel控件的ID,用来存放动态加载的控件 panelSpec.Controls.Add(newFileUpload) ctrlUploadList.Add(newFileUpload) ctrlID += 1 End Sub '通过RestoreControls避免丢掉之前动态加载的控件 Protected Sub RestoreControls() Dim myfileUpload As UserControl_PicUpload For Each myfileUpload In ctrlUploadList If myfileUpload IsNot Nothing Then panelSpec.Controls.Add(myfileUpload) End If Next End Sub Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not IsPostBack Then ... '页面加载时对其他控件进行初始化 End If RestoreControls() End Sub
AddProValue apv = (AddProValue)Page.LoadControl("UserControl/AddProValue.ascx"); apv.Num = Convert.ToInt32(kvp.Key); apv.ProValue = kvp.Value; apv.ID = "apv" + Convert.ToString(kvp.Key); this.ph1.Controls.Add(apv);
------解决方案--------------------
你可以直接写空间, 也可以继承某个控件, 然后编译成dll, 项目引用改dll