.ascx UserControl Properties are set after Page_Load only on PostBacks

D

Douglas J. Badin

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"
<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
 
Y

Yan-Hong Huang[MSFT]

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" <[email protected]>
!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: <#[email protected]>
!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
!
!
!
 
S

Sreenath Madyastha

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
 
D

Douglas J. Badin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top