我想要使用的控件已在我的页面中注册。
<%@ Register Src="~/MEDCONTROLS/statform.ascx" TagPrefix="uc1" TagName="statform" %>
我正在使用此代码在我的页面中显示它。 (使用不同的id&amp;&amp;&amp;&amp; TempHosp#)
<uc1:statform runat="server" TempHospNum="ER101" ID="statform1" />
<uc1:statform runat="server" TempHospNum="ER102" ID="statform2" />
<uc1:statform runat="server" TempHospNum="ER103" ID="statform3" />
现在我需要做的是,在点击按钮事件中,它将创建一个新的用户控件,实际上是相同的用户控件,但只有不同的id和不同的属性,例如。
修改
Protected Sub btnAddUserControl_Click(sender As Object, e As EventArgs) Handles btnAddUserControl.Click
'Generate user control with a TempHospnum = "ER104" and id="statform4"
End Sub
现在它只在page.init中添加BUT,而不是在Button_click上添加
'NOT WORKING
Protected Sub btnNewBed_Click(sender As Object, e As EventArgs) Handles btnNewBed.Click
Dim myControl As statform = Page.LoadControl("~/MEDCONTROLS/statform.ascx")
myControl.ID = "statform13"
myControl.TempHospNum = "ER113"
myControl.ERBedNumber = "Bed13"
form1.Controls.Add(myControl)
myControl.Visible = True
End Sub
'This is working but this is not what I needed
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim myControl As statform = Page.LoadControl("~/MEDCONTROLS/statform.ascx")
myControl.ID = "statform13"
myControl.TempHospNum = "ER113"
myControl.ERBedNumber = "Bed13"
form1.Controls.Add(myControl)
myControl.Visible = True
End Sub
答案 0 :(得分:0)
使用引用用户控件的类名的变量,并使用Page.LoadControl。
您可以与添加到用户控件类定义的任何属性进行交互。
dim myControl as YourUserControlClassName = Page.LoadControl("~/yourlayoutfilepath/yourUserControlFile.ascx")
myControl.ID="stratFormX"
Me.Controls.Add(myControl)