how to access a user control

S

SStory

I made an ascx control

States.

It just has a dropdown combo with a list of state abbrevs.

I dropped it on a webform then in the code behind file I want to get access
to the value that was selected.

Inside of the code file of the states control I have a property
Public ReadOnly Property SelectedItemText() As String
Get
Return dropState.SelectedItem.Text
End Get
End Property

When the control was dropped on the webform it was given the id StateNewLoc

But in the code behind Vb file I can't access any properties by that
name--it won't come up in intellisence.

So I declared a protected variable
Protected StatesAddLoc As States

But StatesAddLoc.SelectedItemText always gives me an error of
nullreference--it is not set to anything.

That makes sense since the name is different from the ID but I don't know
how do I get access to the control on the webform from within the vb code
behind file.

Thanks,

Shane
 
K

Ken Cox [Microsoft MVP]

Here's another way of accessing a user control that doesn't require you to
create your own properties:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim StatesAddLoc As UserControl
Dim dropState As DropDownList
StatesAddLoc = Page.FindControl("StateNewLoc")
dropState = StatesAddLoc.FindControl("dropState")
Label1.Text = dropState.SelectedItem.Text
End Sub

Does this help?

Ken
MVP [ASP.NET]


--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/security_bulletins/ms03-026.asp



I made an ascx control

States.

It just has a dropdown combo with a list of state abbrevs.

I dropped it on a webform then in the code behind file I want to get access
to the value that was selected.

Inside of the code file of the states control I have a property
Public ReadOnly Property SelectedItemText() As String
Get
Return dropState.SelectedItem.Text
End Get
End Property

When the control was dropped on the webform it was given the id StateNewLoc

But in the code behind Vb file I can't access any properties by that
name--it won't come up in intellisence.

So I declared a protected variable
Protected StatesAddLoc As States

But StatesAddLoc.SelectedItemText always gives me an error of
nullreference--it is not set to anything.

That makes sense since the name is different from the ID but I don't know
how do I get access to the control on the webform from within the vb code
behind file.

Thanks,

Shane
 
S

SStory

That is kind of neat, but seems like maybe defeats the purpose of
reusability doesn't it? I made the control to just drop on and have a
common item working without thinking about it again and again--although it
is simple. Also as my first user control I wanted a simple one. It has
turned out to be harder to accomplish than I thought, but with a little help
from MS I almost have everything working.

I am now using the states control on a page in a panel--for adding and in a
datalist on the same page in the EditItemTemplate

The only problem now is it seems I can't initialize the control. I tried
binding a property just like any other control but for some reason it
doesn't...

Thanks Ken for the idea. I will keep it in mind for the future--neat to know
it is doable that way.

Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top