Dynamically added user control

Discussion in 'ASP .Net' started by Chris, Apr 1, 2007.

  1. Chris

    Chris Guest

    I am dynamically adding a user control to each row in a gridview. The reason
    I am doing it dynamically is the user control is different depending on
    certain data in the gridview. The gridview contains a placeholder and I add
    the control to it, the user control is a formview bound to an object
    datsource. This works great until I post back the page and the user control
    disappears. What am I doing wrong? Regards, Chris.



    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

    Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
    ObjectDataSource)

    o.SelectParameters(0).DefaultValue =
    GridView1.DataKeys(e.Row.DataItemIndex).Value

    Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"), PlaceHolder)

    Dim ctl As Control = LoadControl("updateorder.ascx")

    Dim odssub As ObjectDataSource = CType(ctl.FindControl("odscustomer"),
    ObjectDataSource)

    odssub.SelectParameters("customerid").DefaultValue =
    CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value)

    pl.Controls.Add(ctl)

    End If

    End Sub
     
    Chris, Apr 1, 2007
    #1
    1. Advertising

  2. You have to re-create dynamically added controls on every postback,
    preferably in Page_Init event. It could be easier to cater for all possible
    controls in the ItemTemplate and show/hide them as required with css rule
    display:none.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Chris" <> wrote in message
    news:...
    >I am dynamically adding a user control to each row in a gridview. The
    >reason I am doing it dynamically is the user control is different depending
    >on certain data in the gridview. The gridview contains a placeholder and I
    >add the control to it, the user control is a formview bound to an object
    >datsource. This works great until I post back the page and the user control
    >disappears. What am I doing wrong? Regards, Chris.
    >
    >
    >
    > Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    > GridView1.RowDataBound
    >
    > If e.Row.RowType = DataControlRowType.DataRow Then
    >
    > Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
    > ObjectDataSource)
    >
    > o.SelectParameters(0).DefaultValue =
    > GridView1.DataKeys(e.Row.DataItemIndex).Value
    >
    > Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"), PlaceHolder)
    >
    > Dim ctl As Control = LoadControl("updateorder.ascx")
    >
    > Dim odssub As ObjectDataSource = CType(ctl.FindControl("odscustomer"),
    > ObjectDataSource)
    >
    > odssub.SelectParameters("customerid").DefaultValue =
    > CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value)
    >
    > pl.Controls.Add(ctl)
    >
    > End If
    >
    > End Sub
    >
    >
     
    Eliyahu Goldin, Apr 1, 2007
    #2
    1. Advertising

  3. Chris

    Chris Guest

    I kind of like the user control idea as it is a little more modular. I am
    going to end end with about 20 variations of user control, doing it in the
    item template seem more complex. How would I go about adding the user
    controls to a gridview from the page init event. Regards, Chris.

    "Eliyahu Goldin" <> wrote in
    message news:...
    > You have to re-create dynamically added controls on every postback,
    > preferably in Page_Init event. It could be easier to cater for all
    > possible controls in the ItemTemplate and show/hide them as required with
    > css rule display:none.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "Chris" <> wrote in message
    > news:...
    >>I am dynamically adding a user control to each row in a gridview. The
    >>reason I am doing it dynamically is the user control is different
    >>depending on certain data in the gridview. The gridview contains a
    >>placeholder and I add the control to it, the user control is a formview
    >>bound to an object datsource. This works great until I post back the page
    >>and the user control disappears. What am I doing wrong? Regards, Chris.
    >>
    >>
    >>
    >> Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    >> System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    >> GridView1.RowDataBound
    >>
    >> If e.Row.RowType = DataControlRowType.DataRow Then
    >>
    >> Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
    >> ObjectDataSource)
    >>
    >> o.SelectParameters(0).DefaultValue =
    >> GridView1.DataKeys(e.Row.DataItemIndex).Value
    >>
    >> Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"),
    >> PlaceHolder)
    >>
    >> Dim ctl As Control = LoadControl("updateorder.ascx")
    >>
    >> Dim odssub As ObjectDataSource = CType(ctl.FindControl("odscustomer"),
    >> ObjectDataSource)
    >>
    >> odssub.SelectParameters("customerid").DefaultValue =
    >> CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value)
    >>
    >> pl.Controls.Add(ctl)
    >>
    >> End If
    >>
    >> End Sub
    >>
    >>

    >
    >
     
    Chris, Apr 1, 2007
    #3
  4. Chris

    Mark Rae Guest

    "Chris" <> wrote in message
    news:...

    >I kind of like the user control idea as it is a little more modular. I am
    >going to end end with about 20 variations of user control, doing it in the
    >item template seem more complex. How would I go about adding the user
    >controls to a gridview from the page init event.


    Eliyahu is right, though I think you have slightly misunderstood his
    advice...

    Dynamically created controls need to be dynamically created every time the
    page loads - they don not persist across a postback.

    However, you don't add the dynamically created controls to the GridView in
    Page_Init - you simply create them there and hide them for use later.

    Although dynamic controls *can* be created anywhere in code-behind, they
    have a tendency not to work properly if they are created any later in the
    page cycle than Page_Init - specifically, their events don't get wired up
    successfully.

    Thus, when you come to bind your GridView, you can use its OnDataBinding
    event simply to add the dynamically created controls to the GridView as
    required...
     
    Mark Rae, Apr 1, 2007
    #4
  5. Chris

    Chris Guest

    I need help, I've looking at this for too long. I need to add the controls
    on every postback but when I load the control in the init and add it on
    databinding it still disappears on postback. I've been having more success
    with adding the controls on the page load.

    For Each row In GridView1.Rows

    Dim pl As PlaceHolder = CType(row.FindControl("plchildgw"), PlaceHolder)

    pl.Controls.Add(mastercontrol)

    Next

    but the control only binds to the last element in the gridview, which may be
    some thing stupid I have done. Thanks for the help ;)

    Here is the code where I try to bind later

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Init

    mastercontrol = LoadControl("updateorder.ascx")

    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewRowEventArgs) Handles
    GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

    Dim o As ObjectDataSource = CType(e.Row.FindControl("objdsgetorders"),
    ObjectDataSource)

    o.SelectParameters(0).DefaultValue =
    GridView1.DataKeys(e.Row.DataItemIndex).Value

    Dim pl As PlaceHolder = CType(e.Row.FindControl("plchildgw"), PlaceHolder)

    Dim odssub As ObjectDataSource =
    CType(mastercontrol.FindControl("odscustomer"), ObjectDataSource)

    odssub.SelectParameters("customerid").DefaultValue =
    CInt(GridView1.DataKeys(e.Row.DataItemIndex).Value)

    pl.Controls.Add(mastercontrol)

    End If

    End Sub



    "Mark Rae" <> wrote in message
    news:%...
    > "Chris" <> wrote in message
    > news:...
    >
    >>I kind of like the user control idea as it is a little more modular. I am
    >>going to end end with about 20 variations of user control, doing it in the
    >>item template seem more complex. How would I go about adding the user
    >>controls to a gridview from the page init event.

    >
    > Eliyahu is right, though I think you have slightly misunderstood his
    > advice...
    >
    > Dynamically created controls need to be dynamically created every time the
    > page loads - they don not persist across a postback.
    >
    > However, you don't add the dynamically created controls to the GridView in
    > Page_Init - you simply create them there and hide them for use later.
    >
    > Although dynamic controls *can* be created anywhere in code-behind, they
    > have a tendency not to work properly if they are created any later in the
    > page cycle than Page_Init - specifically, their events don't get wired up
    > successfully.
    >
    > Thus, when you come to bind your GridView, you can use its OnDataBinding
    > event simply to add the dynamically created controls to the GridView as
    > required...
    >
     
    Chris, Apr 1, 2007
    #5
  6. Chris

    Mark Rae Guest

    "Chris" <> wrote in message
    news:...

    > Here is the code where I try to bind later
    >
    > Protected Sub Page_Init(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Init
    >
    > mastercontrol = LoadControl("updateorder.ascx")
    >
    > End Sub


    That creates *one* dynamic control.

    > but the control only binds to the last element in the gridview, which may
    > be some thing stupid I have done.


    You need as many dynamic controls as there are rows in the GridView...
     
    Mark Rae, Apr 1, 2007
    #6
  7. Chris

    Chris Guest

    I've got it working with this. It makes sense to me but if you can see
    potential problems.....

    Dim row As GridViewRow

    Dim i As Integer = 0

    For Each row In GridView1.Rows

    Dim txt As New TextBox()

    Dim control As New Control()

    mastercontrol = LoadControl("updateorder.ascx")

    Dim odssub As ObjectDataSource =
    CType(mastercontrol.FindControl("odscustomer"), ObjectDataSource)

    odssub.SelectParameters("customerid").DefaultValue =
    GridView1.DataKeys(row.RowIndex).Value

    Dim pl As PlaceHolder = CType(row.FindControl("plchildgw"), PlaceHolder)

    pl.Controls.Add(mastercontrol)

    Next




    "Mark Rae" <> wrote in message
    news:...
    > "Chris" <> wrote in message
    > news:...
    >
    >> Here is the code where I try to bind later
    >>
    >> Protected Sub Page_Init(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Init
    >>
    >> mastercontrol = LoadControl("updateorder.ascx")
    >>
    >> End Sub

    >
    > That creates *one* dynamic control.
    >
    >> but the control only binds to the last element in the gridview, which may
    >> be some thing stupid I have done.

    >
    > You need as many dynamic controls as there are rows in the GridView...
    >
     
    Chris, Apr 1, 2007
    #7
  8. Chris

    Mark Rae Guest

    "Chris" <> wrote in message
    news:...

    > Dim txt As New TextBox()
    > Dim control As New Control()


    What are those two variables for...? You don't appear to be using them
    anywhere...
     
    Mark Rae, Apr 1, 2007
    #8
  9. Chris

    Chris Guest

    They've been left in by accident :)

    "Mark Rae" <> wrote in message
    news:...
    > "Chris" <> wrote in message
    > news:...
    >
    >> Dim txt As New TextBox()
    >> Dim control As New Control()

    >
    > What are those two variables for...? You don't appear to be using them
    > anywhere...
    >
     
    Chris, Apr 1, 2007
    #9
  10. Chris

    Mark Rae Guest

    "Chris" <> wrote in message
    news:...

    > They've been left in by accident :)


    Ah... :)
     
    Mark Rae, Apr 1, 2007
    #10
    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. Bennett Haselton
    Replies:
    1
    Views:
    539
    John M Deal
    Nov 8, 2004
  2. Jon B
    Replies:
    5
    Views:
    5,249
  3. =?Utf-8?B?U2FtdWVs?=
    Replies:
    7
    Views:
    1,067
    Scott Allen
    Aug 10, 2005
  4. Replies:
    1
    Views:
    445
    Teemu Keiski
    Jan 3, 2006
  5. Replies:
    3
    Views:
    294
    Lloyd Sheen
    Jun 8, 2007
Loading...

Share This Page