Groups | Blog | Home
all groups > vb.net controls > july 2005 >

vb.net controls : Getting Values from a User Control


graham bates
7/13/2005 2:15:44 PM
Hi There,

In desperation I was wondering whether anyone could cast their eye over
a small problem I having. I am attempting to create a user control
which basically consists of a DropDown list. Now, I wish to both
retrieve values from this drop down list, and also to change the values
that form the drop down list collection. I havent even managed the
first of these tasks yet, even though I follow the get/set logic of
what I need to do, so I was hoping if I pasted a small sample piece of
code, someone could let me know where I am going wrong.

Rather than cut and pasting the code here, I have created the following
page;

http://www.asthecrowflies.co.uk/dropdownusercontrol.htm

Which shows my pages, plus the code behind. It would appear that when I
try to get the value of the text box (My sample code attempts to print
the selected value into the label), it fails, with the value seemingly
not being retrieved, and therefore the label.text is being written to
with a null value.

Because I have kept the example so simple I was hoping it would appear
really obvious to someone where I was going wrong - if anyone has any
ideas, please could you reply to the group.

Many Thanks,

Graham
Ken Tucker [MVP]
7/14/2005 6:54:54 AM
Hi,

I am suprised the web application doesnt crash. You never assign a
value to myWebUserControl. Try this.

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
Dim TestControl As Control
TestControl = LoadControl("WebUserControl1.ascx")
PlaceHolder1.Controls.Add(TestControl)
myWebUserControl = CType(TestControl, WebUserControl1)
End Sub

Since the drop property in the usercontrol is readonly I would
change the code to this.

Public ReadOnly Property drop() As String
Get
Return DropDownList1.SelectedValue
End Get
End Property


Ken
---------------------
[quoted text, click to view]
Hi There,

In desperation I was wondering whether anyone could cast their eye over
a small problem I having. I am attempting to create a user control
which basically consists of a DropDown list. Now, I wish to both
retrieve values from this drop down list, and also to change the values
that form the drop down list collection. I havent even managed the
first of these tasks yet, even though I follow the get/set logic of
what I need to do, so I was hoping if I pasted a small sample piece of
code, someone could let me know where I am going wrong.

Rather than cut and pasting the code here, I have created the following
page;

http://www.asthecrowflies.co.uk/dropdownusercontrol.htm

Which shows my pages, plus the code behind. It would appear that when I
try to get the value of the text box (My sample code attempts to print
the selected value into the label), it fails, with the value seemingly
not being retrieved, and therefore the label.text is being written to
with a null value.

Because I have kept the example so simple I was hoping it would appear
really obvious to someone where I was going wrong - if anyone has any
ideas, please could you reply to the group.

Many Thanks,

Graham

AddThis Social Bookmark Button