how to access a user control

Discussion in 'ASP .Net Web Controls' started by SStory, Sep 8, 2003.

  1. SStory

    SStory Guest

    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
     
    SStory, Sep 8, 2003
    #1
    1. Advertising

  2. 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



    "SStory" <> wrote in message
    news:...
    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
     
    Ken Cox [Microsoft MVP], Sep 9, 2003
    #2
    1. Advertising

  3. SStory

    SStory Guest

    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

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "SStory" <> wrote in message
    > news:...
    > 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
    >
    >
    >
     
    SStory, Sep 10, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwerty
    Replies:
    3
    Views:
    9,325
    Scott Allen
    Sep 30, 2004
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    674
    Mad Scientist Jr
    Mar 22, 2006
  3. Chris Zopers

    Access user control from other user control

    Chris Zopers, Feb 20, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    588
    Nanda Lella[MSFT]
    Apr 27, 2009
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    580
    Jonathan Wood
    Feb 2, 2010
  5. Faizan Ahmed
    Replies:
    1
    Views:
    308
    John Saunders
    Jan 4, 2005
Loading...

Share This Page