Dynamically added usercontrol raised events

Discussion in 'ASP .Net Building Controls' started by JC, Oct 12, 2004.

  1. JC

    JC Guest

    I've tried and tried and this problem eludes me.
    I have a form called "main" which has a div tag "DeptInfo" in which I
    add my control "Information", here's the code.

    ---
    Dim cont As New Control
    cont = LoadControl("Information.ascx")
    cont.ID = "Information"
    DeptInfo.Controls.Add(cont)
    ---


    At the top of main I added
    ---
    Protected WithEvents Information As Information
    ---

    In main I added
    ---
    Private Sub Information_ProductSelectionChanged(ByVal sender As
    Object, ByVal e As CommandEventArgs) Handles
    Information.ProductSelectionChanged
    'do my stuff here
    End Sub
    ---

    In the control information I have at the top
    ---
    Public Event ProductSelectionChanged(ByVal sender As Object, ByVal e
    As CommandEventArgs)
    ---

    and raise the event on sel index changed for a ddl:
    ---
    Dim evnt As New CommandEventArgs(DropDownList1.Items(DropDownList1.SelectedIndex).Text,
    Nothing)
    RaiseEvent ProductSelectionChanged(Me, evnt)
    ---

    Here's the problem, this ALL works if the control as added at design
    time by dragging the control onto the form. The event
    "Information_ProductSelectionChanged" on form main fires.

    How can I wire this up when adding the control dynamically?
    How can I reference "main" from "information" and pass info without
    having to generate an event?

    Thanks for any help!!!
     
    JC, Oct 12, 2004
    #1
    1. Advertising

  2. JC

    JC Guest

    For anyone having a similar problem here's what I did...

    Dim WithEvents myUC1 As webusercontrol1

    myUC1 = LoadControl("webusercontrol1.ascx")
    myUC1 .ID = "myuc1id"
    placeHolder1.Controls.Add(myUC1)

    The diference is that I did not call the new() function and create
    another control. Also I made public properties on the usercontrol to
    pass info down to the usercontrol.

    My thanks to Scott Mitchell from 4GuysFromRolla.com. It was very nice
    of him to lend me a hand. If you're ever stuck look to their site!

    Got to give credit where it is due.
    Jeff


    (JC) wrote in message news:<>...
    > I've tried and tried and this problem eludes me.
    > I have a form called "main" which has a div tag "DeptInfo" in which I
    > add my control "Information", here's the code.
    >
    > ---
    > Dim cont As New Control
    > cont = LoadControl("Information.ascx")
    > cont.ID = "Information"
    > DeptInfo.Controls.Add(cont)
    > ---
    >
    >
    > At the top of main I added
    > ---
    > Protected WithEvents Information As Information
    > ---
    >
    > In main I added
    > ---
    > Private Sub Information_ProductSelectionChanged(ByVal sender As
    > Object, ByVal e As CommandEventArgs) Handles
    > Information.ProductSelectionChanged
    > 'do my stuff here
    > End Sub
    > ---
    >
    > In the control information I have at the top
    > ---
    > Public Event ProductSelectionChanged(ByVal sender As Object, ByVal e
    > As CommandEventArgs)
    > ---
    >
    > and raise the event on sel index changed for a ddl:
    > ---
    > Dim evnt As New CommandEventArgs(DropDownList1.Items(DropDownList1.SelectedIndex).Text,
    > Nothing)
    > RaiseEvent ProductSelectionChanged(Me, evnt)
    > ---
    >
    > Here's the problem, this ALL works if the control as added at design
    > time by dragging the control onto the form. The event
    > "Information_ProductSelectionChanged" on form main fires.
    >
    > How can I wire this up when adding the control dynamically?
    > How can I reference "main" from "information" and pass info without
    > having to generate an event?
    >
    > Thanks for any help!!!
     
    JC, Oct 13, 2004
    #2
    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. Brad
    Replies:
    0
    Views:
    436
  2. Brad
    Replies:
    1
    Views:
    719
    Steven Cheng[MSFT]
    Mar 31, 2005
  3. OJ
    Replies:
    3
    Views:
    1,334
    Karl Seguin [MVP]
    May 16, 2006
  4. Craig
    Replies:
    0
    Views:
    143
    Craig
    Sep 8, 2003
  5. Richard Steele

    Missing UserControl When Added To Wizard As A UserControl

    Richard Steele, Dec 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    108
    Richard Steele
    Dec 23, 2005
Loading...

Share This Page