Dynamically Loading User Control in PlaceHolder

Discussion in 'ASP .Net Building Controls' started by Ugur Ekinci, Nov 22, 2005.

  1. Ugur Ekinci

    Ugur Ekinci Guest

    Hi ,
    I load dynamically a user control in PlaceHolder with the number of fileds
    in db.
    When loading user control there is no problem, if returning field number is
    2 then 2 usr controls load in plcholder.
    But problem is that , after page postback plcholder says that I have no
    control,

    For i As Integer = 0 To plcHolderUsrCont.Controls.Count - 1
    kontrol1 = DirectCast(plcHolderUsrCont.Controls(i),
    UrunGrupTeknik)
    Dim a As String = kontrol1.txtvalue
    Next

    I added a reference like below , because usr control loading dynamically
    <%@ Reference Control = "../kontroller/UrunGrupTeknik.ascx" %>

    I defined a protected variable that's type is my usr control
    Protected urunGrupTeknikTxtLbl As UrunGrupTeknik = New UrunGrupTeknik

    and I load user control like that..

    For i As Integer = 0 To tdsTeknikDetay.table1.Rows.Count - 1
    thisrowTeknikDetay = tdsTeknikDetay.table1.Rows(i)
    findControlPath = Request.ApplicationPath +
    "/kontroller/UrunGrupTeknik.ascx"
    kontrol1 = Me.LoadControl(findControlPath)
    urunGrupTeknikTxtLbl = DirectCast(kontrol1, UrunGrupTeknik)
    urunGrupTeknikTxtLbl.lblValue = thisrowTeknikDetay.ACIKLAMA
    kontrol1.ID = i
    plcHolderUsrCont.Controls.Add(kontrol1)
    Next

    plcHolder.controls.Count is set to 0 after postback:((
    Have you any idea about this problem ?

    Thnks....
     
    Ugur Ekinci, Nov 22, 2005
    #1
    1. Advertising

  2. Ugur Ekinci

    DS Guest

    Hi,

    I have a asp table in my .aspx page. I dynamically add the control to this
    table. The page shows up fine. The control I had have 2 textboxes. Now I am
    stuck and not able to access any of these textboxes inside the control I
    added. Infact, i am not able to locate this control in the page.

    Please help me out.

    D. Santhosh

    "Ugur Ekinci" wrote:

    > Hi ,
    > I load dynamically a user control in PlaceHolder with the number of fileds
    > in db.
    > When loading user control there is no problem, if returning field number is
    > 2 then 2 usr controls load in plcholder.
    > But problem is that , after page postback plcholder says that I have no
    > control,
    >
    > For i As Integer = 0 To plcHolderUsrCont.Controls.Count - 1
    > kontrol1 = DirectCast(plcHolderUsrCont.Controls(i),
    > UrunGrupTeknik)
    > Dim a As String = kontrol1.txtvalue
    > Next
    >
    > I added a reference like below , because usr control loading dynamically
    > <%@ Reference Control = "../kontroller/UrunGrupTeknik.ascx" %>
    >
    > I defined a protected variable that's type is my usr control
    > Protected urunGrupTeknikTxtLbl As UrunGrupTeknik = New UrunGrupTeknik
    >
    > and I load user control like that..
    >
    > For i As Integer = 0 To tdsTeknikDetay.table1.Rows.Count - 1
    > thisrowTeknikDetay = tdsTeknikDetay.table1.Rows(i)
    > findControlPath = Request.ApplicationPath +
    > "/kontroller/UrunGrupTeknik.ascx"
    > kontrol1 = Me.LoadControl(findControlPath)
    > urunGrupTeknikTxtLbl = DirectCast(kontrol1, UrunGrupTeknik)
    > urunGrupTeknikTxtLbl.lblValue = thisrowTeknikDetay.ACIKLAMA
    > kontrol1.ID = i
    > plcHolderUsrCont.Controls.Add(kontrol1)
    > Next
    >
    > plcHolder.controls.Count is set to 0 after postback:((
    > Have you any idea about this problem ?
    >
    > Thnks....
    >
    >
    >
    >
     
    DS, Nov 22, 2005
    #2
    1. Advertising

  3. This is a classic issue.

    ASP.NET will not persist your dynamically created controls by default, it
    will make sure the control-tree at the .aspx will be regenerated but thats
    all, no automatic support for your codebehind. They are simply won't be
    written to Viewstate tree, and hence corresponding server side control won't
    be re-created after a postback.

    You may use a helper control like this one instead of the PlaceHolder:
    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

    Gokhan Altinoren
    gokhan[RMV_this][at]altinoren[also_RMV_this][dot]com
    http://altinoren.com

    > Hi ,
    > I load dynamically a user control in PlaceHolder with the number of
    > fileds
    > in db.
    > When loading user control there is no problem, if returning field
    > number is
    > 2 then 2 usr controls load in plcholder.
    > But problem is that , after page postback plcholder says that I have
    > no
    > control,
    > For i As Integer = 0 To plcHolderUsrCont.Controls.Count - 1
    > kontrol1 = DirectCast(plcHolderUsrCont.Controls(i),
    > UrunGrupTeknik)
    > Dim a As String = kontrol1.txtvalue
    > Next
    > I added a reference like below , because usr control loading
    > dynamically <%@ Reference Control =
    > "../kontroller/UrunGrupTeknik.ascx" %>
    >
    > I defined a protected variable that's type is my usr control
    > Protected urunGrupTeknikTxtLbl As UrunGrupTeknik = New UrunGrupTeknik
    >
    > and I load user control like that..
    >
    > For i As Integer = 0 To tdsTeknikDetay.table1.Rows.Count - 1
    > thisrowTeknikDetay = tdsTeknikDetay.table1.Rows(i)
    > findControlPath = Request.ApplicationPath +
    > "/kontroller/UrunGrupTeknik.ascx"
    > kontrol1 = Me.LoadControl(findControlPath)
    > urunGrupTeknikTxtLbl = DirectCast(kontrol1,
    > UrunGrupTeknik)
    > urunGrupTeknikTxtLbl.lblValue =
    > thisrowTeknikDetay.ACIKLAMA
    > kontrol1.ID = i
    > plcHolderUsrCont.Controls.Add(kontrol1)
    > Next
    > plcHolder.controls.Count is set to 0 after postback:(( Have you any
    > idea about this problem ?
    >
    > Thnks....
    >
     
    Gokhan Altinoren, Nov 27, 2005
    #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. Stu
    Replies:
    1
    Views:
    616
    Karl Seguin
    Jan 6, 2005
  2. Bert
    Replies:
    3
    Views:
    1,965
    Juan T. Llibre
    Dec 12, 2006
  3. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    372
  4. Phoenix
    Replies:
    2
    Views:
    258
    Teemu Keiski
    Jun 6, 2005
  5. Object Delayed loading dynamically into Placeholder

    , Oct 5, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    142
Loading...

Share This Page