wow... you are gonna love me because i am going to make your life so
much easier. Please forgive me if I screw up my vb.net syntax though.
I'll paste the relevant code first...
[User Control]
Public Class ddl
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Public WithEvents DropDownList1 As
System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class
[WebForm]
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents postbackbutton As
System.Web.UI.WebControls.Button
Protected WithEvents result As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Protected WithEvents DDLUC As ddl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub postbackbutton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles postbackbutton.Click
DDLUC = Page.FindControl("MyUC")
result.Text = DDLUC.DropDownList1.SelectedValue
End Sub
Ok, so basically what you need to look at is that in the UC i have
DropDownList1 (the the only one on the page) as public, rather than
protected which is default. On the webform, i have declared Protected
WithEvents DDLUC as ddl. ddl is the uc's class (public class ddl).
Now, on my webform i have 3 items- the user contol, the button, and the
label. on my Click event, i set DDLUC (my protected class variable)
equal to the value of the user control (MyUC is the ID of the control
in the webform). Then you just access the DropDownList1 to get the
selected value.
You can take this further by keeping the dropdownlist1 as protected and
having properties that return what you want. However you do it, you
can do it without all that reflection mumbo jumbo.
HTH,
Darren Kopp
http://blog.secudocs.com/