[2005] UserControl LifeCycle
Hi All,
I have a masterpage that has a usercontrol on it that contains a treeview, I also have a content page inside the masterpage that contains a usercontrol and a gridview in that usercontrol.
I am having problems in that my TreeView is populating a Session object with a categoryID that is then used to populate the datasource for the gridview in the content page. However the SelectedNodeChanged event of the TreeView is occuring AFTER the Load event of the usercontrol that contains my GridView, so basically what occurs is my binding is always one step behind my the actual selected categoryId.
The workaround I have used is basically raising a series of events from the treeview usercontrol to the masterpage and back to the gridview usercontrol.
I find this to be an ugly solution and was hoping that someone could suggest something better.
If I havent been completely clear, please let me know and I will clarify...
Thanks in advance for any input.
Re: [2005] UserControl LifeCycle
Quote:
Originally Posted by maximg
However the SelectedNodeChanged event of the TreeView is occuring AFTER the Load event of the usercontrol that contains my GridView, so basically what occurs is my binding is always one step behind my the actual selected categoryId.
So if I am understanding you correctly you're doing your DataBind during the Loading of the control, correct?
If I am correct and you need to change the data for the DataBind depending on what's selected on the client side then why not keep your DataBind in the load event, but check and make sure the page is not being posted back before doing the DataBind. If the page is doing a PostBack then you'll call DataBind from the SelectedNodeChanged event.
Re: [2005] UserControl LifeCycle
Are these pages with the gridview category-centric? Why not just put the categoryID in the URL? It's almost like a navigation from the sounds of it.
Re: [2005] UserControl LifeCycle
Thanks both for your replies.
Kasracer: Yes I am doing my binding in the Load event of the Usercontrol however the Load event of the usercontrol that contains the gridview is occuring BEFORE the SelectedNodeChanged event is occuring the TreeView..
Let me give you a more clear example.
[Example: UserControl1 Containing TreeView]
Code:
Protected Sub tvMainTree_SelectedNodeChanged
Session("CategoryID") = DirectCast(sender, TreeView).SelectedValue
End Sub
[Example: UserControl2 Containing GridView]
Code:
Protected Sub Page_Load
If Not IsPostBack Then
Call RebindTaskArea()
End If
End Sub
Public Sub RebindTaskArea()
MyGridView.DataSource = Customer.CustomerTasksByCategory(CInt(Session("CategoryID")), My.Settings.DBConnString)
MyGridView.DataBind()
End Sub
So basically the problem is the example 2 is being called before example 1 so it always has the previous CategoryID.
mendhak: This is not a multi page website (at least not the part that is being affected by these problems), there is only a Default.aspx which contains a number of usercontrols. (Imagine all this is a web based SIMPLE task manager I am writing for myself) Passing the CategoryID in the QueryString I suppose could still work but I feel the QueryString is also generally poor implementation with ASP.Net 2. Am I incorrect?
Re: [2005] UserControl LifeCycle
Quote:
Originally Posted by maximg
Kasracer: Yes I am doing my binding in the Load event of the Usercontrol however the Load event of the usercontrol that contains the gridview is occuring BEFORE the SelectedNodeChanged event is occuring the TreeView..
I understand that the Load event is happening before the SelectedNodeChanged event. This is by design. Every-time your page is requested (either by navigating to it or via a post back) the page and all of its controls are re-created. It's only natural that your control is loaded / initialized before any events are called (otherwise your control would be null).
You need to re-think your design to fit into the Page and Control Life Cycles. Instead of doing your DataBind in the load event every-time you should only do so when not in a post back. If it's in a post-back then call DataBind in the SelectedNodeChanged.
Look at this; the event will only occur during post-back so call the DataBind event in there:
[UserControl1 Containing TreeView]
Code:
Protected Sub tvMainTree_SelectedNodeChanged
Session("CategoryID") = DirectCast(sender, TreeView).SelectedValue
Call RebindTaskArea()
End Sub
[UserControl2 Containing GridView]
Code:
Protected Sub Page_Load
If Not IsPostBack Then
Call RebindTaskArea()
End If
End Sub
Public Sub RebindTaskArea()
MyGridView.DataSource = Customer.CustomerTasksByCategory(CInt(Session("CategoryID")), My.Settings.DBConnString)
MyGridView.DataBind()
End Sub
Re: [2005] UserControl LifeCycle
Quote:
Originally Posted by maximg
mendhak: This is not a multi page website (at least not the part that is being affected by these problems), there is only a Default.aspx which contains a number of usercontrols. (Imagine all this is a web based SIMPLE task manager I am writing for myself) Passing the CategoryID in the QueryString I suppose could still work but I feel the QueryString is also generally poor implementation with ASP.Net 2. Am I incorrect?
In what way is it a poor implementation? It doesn't get any more complicated than Request.Querystring("fieldname").
I'm just saying that if the site is based around these category items such that clicking on Category9 determines the content of the gridviews and other controls, then it's perfectly valid to have it in the querystring.
Imagine if VBForums decided to do everything by sessions. Then this page would be showthread.php. You'd never find a way of sending this page to your friend or finding it again unless you came to the home page and started clicking your way through. Forumdisplay.php uses a querystring, in this case, f=31, so that it can load all the ASP.NET threads. It's the ForumID which is driving the content here, so that makes it valid.