.ascx UserControl Properties are set after Page_Load only on PostBacks

Discussion in 'ASP .Net Building Controls' started by Douglas J. Badin, Oct 13, 2003.

  1. I wanted to have a hierarchical DataGrid so I created a User Control that
    has a DataGrid to act as the Child Grid. I put it inside a pageable Parent
    Grid with a page size of one as below in Parent.aspx.

    <%@ Register Tagprefix="ABC" Tagname="ChildDataGrid" Src="ucChild.ascx" %>

    <ASP:DATAGRID
    id="dgParent"
    runat="server"
    showfooter="true"
    allowpaging="true"
    pagesize="1"
    OnPageIndexChanged="dgParent_PageIndexChanged"
    datakeyfield="PK"
    datamember="Parent"
    datasource="<%# DataSet1 %>"
    >

    <COLUMNS>
    <ASP:TEMPLATECOLUMN runat="server" headertext="">
    <ITEMTEMPLATE>

    ....

    <ABC:ChildDataGrid
    id="ucChild1"
    runat="server"
    FK='<%# Convert.ToInt32(DataBinder.Eval(Container, "DataItem.PK")) %>'
    />

    ....

    </ITEMTEMPLATE>
    </ASP:TEMPLATECOLUMN>
    </COLUMNS>
    </ASP:DATAGRID>

    I have properties, "FK" for the User Control (ucChild.ascx) that hold the
    values of the relationship between the Parent and Child Grid. These are
    assigned in the Parent.aspx

    Initially, I DataBind the ParentDataGrid, which sets the properties of the
    User Control and then the Page_Load in the UserControl DataBinds the Child
    as follows

    1 - Parent.aspx.cs: Page_Load: ParentDataGrid.DataBind()
    2 - ucChild.ascx.cs: Properties are set because values are assigned to the
    "ucChild1" attributes in the Parent.aspx
    3 - ucChild.ascx.cs: Page_Load: ChildDataGrid.DataBind()

    No problems so far.

    Paging in the Parent Control causes a Page_Load with PostBack to the Parent
    Page and User Control. It then fires the PageIndexChanged event which sets
    the CurrentPageIndex and re-binds the Parent. This causes another Page_Load
    in the User Control which is also viewed as a Post Back and then the
    properties of the User Control are set.

    1 - Parent.aspx.cs: Page_Load with PostBack
    2 - ucChild.ascx.cs; Page_Load with PostBack
    3 - Parent.aspx.cs: dgParent_PageIndexChanged: Set CurrentPageIndex
    4 - ucChild.ascx.cs: Page_Load with PostBack: ChildDataGrid.DataBind()
    5 - ucChild.ascx.cs: Properties are set because values are assigned to the
    "ucChild1" attributes in the Parent.aspx

    PROBLEM: In the Life-Cycle, the properties were set after the Page_Load on a
    PostBack.

    For now I have put the ucChild.ascx.cs PostBack code in the PreRender and
    things seem to be working.

    QUESTION 1: Maybe I should add a RaisePostBackEvent and do it there?

    QUESTION 2: Why the difference of when the ucChild.ascx.cs properties are
    set depending on PostBack or not?

    Thanks,
    Doug
    Douglas J. Badin, Oct 13, 2003
    #1
    1. Advertising

  2. Hello Douglas,

    Looking at the nature of this issue, I would highly recommend you contact
    Microsoft Product Support Services since it would require intensive
    troubleshooting which would be done quickly and effectively with direct
    assistance from a Microsoft Support Professional.

    Microsoft support home page: http://support.microsoft.com.

    To view support options:
    http://support.microsoft.com/default.aspx?scid=sz;en-us;top.

    To submit an online request:
    http://support.microsoft.com/default.aspx?scid=fh;en-us;incidentsubmit.

    Thanks very much.

    Best regards,
    Yanhong Huang
    Microsoft Online Partner Support

    Get Secure! ┬ĘC www.microsoft.com/security
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    !From: "Douglas J. Badin" <>
    !Subject: .ascx UserControl Properties are set after Page_Load only on
    PostBacks
    !Date: Mon, 13 Oct 2003 12:31:25 -0400
    !Lines: 79
    !X-Priority: 3
    !X-MSMail-Priority: Normal
    !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    !Message-ID: <#>
    !Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    !NNTP-Posting-Host: fll-vodsl60-cust180.mpowercom.net 208.57.60.180
    !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    !Xref: cpmsftngxa06.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:7746
    !X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    !
    !I wanted to have a hierarchical DataGrid so I created a User Control that
    !has a DataGrid to act as the Child Grid. I put it inside a pageable Parent
    !Grid with a page size of one as below in Parent.aspx.
    !
    !<%@ Register Tagprefix="ABC" Tagname="ChildDataGrid" Src="ucChild.ascx" %>
    !
    !<ASP:DATAGRID
    ! id="dgParent"
    ! runat="server"
    ! showfooter="true"
    ! allowpaging="true"
    ! pagesize="1"
    ! OnPageIndexChanged="dgParent_PageIndexChanged"
    ! datakeyfield="PK"
    ! datamember="Parent"
    ! datasource="<%# DataSet1 %>"
    !>
    ! <COLUMNS>
    ! <ASP:TEMPLATECOLUMN runat="server" headertext="">
    ! <ITEMTEMPLATE>
    !
    !...
    !
    ! <ABC:ChildDataGrid
    ! id="ucChild1"
    ! runat="server"
    ! FK='<%# Convert.ToInt32(DataBinder.Eval(Container, "DataItem.PK")) %>'
    ! />
    !
    !...
    !
    ! </ITEMTEMPLATE>
    ! </ASP:TEMPLATECOLUMN>
    ! </COLUMNS>
    !</ASP:DATAGRID>
    !
    !I have properties, "FK" for the User Control (ucChild.ascx) that hold the
    !values of the relationship between the Parent and Child Grid. These are
    !assigned in the Parent.aspx
    !
    !Initially, I DataBind the ParentDataGrid, which sets the properties of the
    !User Control and then the Page_Load in the UserControl DataBinds the Child
    !as follows
    !
    !1 - Parent.aspx.cs: Page_Load: ParentDataGrid.DataBind()
    !2 - ucChild.ascx.cs: Properties are set because values are assigned to the
    !"ucChild1" attributes in the Parent.aspx
    !3 - ucChild.ascx.cs: Page_Load: ChildDataGrid.DataBind()
    !
    !No problems so far.
    !
    !Paging in the Parent Control causes a Page_Load with PostBack to the Parent
    !Page and User Control. It then fires the PageIndexChanged event which sets
    !the CurrentPageIndex and re-binds the Parent. This causes another
    Page_Load
    !in the User Control which is also viewed as a Post Back and then the
    !properties of the User Control are set.
    !
    !1 - Parent.aspx.cs: Page_Load with PostBack
    !2 - ucChild.ascx.cs; Page_Load with PostBack
    !3 - Parent.aspx.cs: dgParent_PageIndexChanged: Set CurrentPageIndex
    !4 - ucChild.ascx.cs: Page_Load with PostBack: ChildDataGrid.DataBind()
    !5 - ucChild.ascx.cs: Properties are set because values are assigned to the
    !"ucChild1" attributes in the Parent.aspx
    !
    !PROBLEM: In the Life-Cycle, the properties were set after the Page_Load on
    a
    !PostBack.
    !
    !For now I have put the ucChild.ascx.cs PostBack code in the PreRender and
    !things seem to be working.
    !
    !QUESTION 1: Maybe I should add a RaisePostBackEvent and do it there?
    !
    !QUESTION 2: Why the difference of when the ucChild.ascx.cs properties are
    !set depending on PostBack or not?
    !
    !Thanks,
    !Doug
    !
    !
    !
    Yan-Hong Huang[MSFT], Oct 14, 2003
    #2
    1. Advertising

  3. Hey

    Thats very easy.,

    Use event handlers. you implement event handlres in the user controls.
    Subscribe to that in the page where u added the usercontrol.

    So u fire the event in the user control automatically u can call method
    in the page. And you can control the method calls.


    hope u got it.

    Regards

    Sreenath



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Sreenath Madyastha, Oct 14, 2003
    #3
  4. Hi Sreenath,

    If I understand you correctly you are talking about communication in the
    other direction, the User Control calling the Hosting Page. My issue is the
    Hosting Page setting User Control Properties, not the User Control setting
    Hosting Page Properties.

    My problem is a Page Life Cyle issue and where properties are set in that
    cycle. The hosting page is setting properties in the User Control via the
    User Control html tag attributes. Sometimes the properties are set before
    the User Control Page_Load and sometimes after the User Control Page_Load
    depending on whether it is a PostBack or not. Not at all consistent which
    is why I am trying to get a better understanding of why so I can avoid
    future problems.

    Thanks,
    Doug


    "Sreenath Madyastha" <> wrote in message
    news:...
    > Hey
    >
    > Thats very easy.,
    >
    > Use event handlers. you implement event handlres in the user controls.
    > Subscribe to that in the page where u added the usercontrol.
    >
    > So u fire the event in the user control automatically u can call method
    > in the page. And you can control the method calls.
    >
    >
    > hope u got it.
    >
    > Regards
    >
    > Sreenath
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Douglas J. Badin, Oct 14, 2003
    #4
    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. z. f.
    Replies:
    0
    Views:
    551
    z. f.
    Oct 19, 2004
  2. rockdale
    Replies:
    1
    Views:
    650
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Nov 16, 2006
  3. Replies:
    0
    Views:
    436
  4. Richard Murphy
    Replies:
    0
    Views:
    155
    Richard Murphy
    Aug 7, 2003
  5. Replies:
    2
    Views:
    525
Loading...

Share This Page