So, I have a GridView inside a class - Let's call this class 'Archives'. I have a calendar on a second class('Sidebar'). From the code-behind for the Sidebar (sidebar.ascx.cs), I want programatic access to the GridView. Specifically, there's a method
inside Archives.ascx.cs which I wish to execute, but I'm flexible as to the location of the method as long as it has access to the GridView.
Both controls are being loaded on the same page (default.aspx). Logically, i would expect to use something like
this.parent.Archives1.GridView1 or control myArch = this.parent.FindControl("Archives1"); myArch.GridView1... .
My success has been limited; I've heard something about passing an instance of the class or constructor, but am not certain how to do this. Thoughts?
-
-
I would highly recommend against a usercontrol accessing another on the page, let alone a control contains within a sibling usercontrol. By definition, a usercontrol can be re-used - what if the page its used on doesnt have a gridview?
You need to have a think about why you need it to have access. For example, if you need
to rebind something in the gridview then I would consider raising an event in Sidebar that is caught by the page, which then in turn rebinds Archives. Alternatively, as a less eloquent solution, you coud raise a bubble event (Control.RaiseBubbleEvent) using a CommandEventArgs and catch OnBubbleEvent on the main page, and rebind the Archives UC from there.
If you REALLY REALLY must use FindControl (and I really do recommend that you think about implementing it properly), then you need to seperate the IDs by either : or $. Eg.
// this == Sidebar
this.Parent.FindControl("Archives1:GridView1"); -
You can throw an event from the sidebar that your page will catch and update the grid accordingly.
Rotem -
PreachingLlama wrote:I would highly recommend against a usercontrol accessing another on the page, let alone a control contains within a sibling usercontrol. By definition, a usercontrol can be re-used - what if the page its used on doesnt have a gridview?
You need to have a think about why you need it to have access. For example, if you need
to rebind something in the gridview then I would consider raising an event in Sidebar that is caught by the page, which then in turn rebinds Archives. Alternatively, as a less eloquent solution, you coud raise a bubble event (Control.RaiseBubbleEvent) using a CommandEventArgs and catch OnBubbleEvent on the main page, and rebind the Archives UC from there.
If you REALLY REALLY must use FindControl (and I really do recommend that you think about implementing it properly), then you need to seperate the IDs by either : or $. Eg.
// this == Sidebar
this.Parent.FindControl("Archives1:GridView1");
Well, let me explain my reasoning and maybe you can give me a better solution. In the current rewrite of my blog I'm working on (http://blog.damnednice.com/dev), the SideBar contains a calendar which highlights all the dates that contain entries. I want the user to be able to click on a day, week, or month, and be taken to the archives page which shall automatically populate a list of all entries within that day, week, or month.
As it stands, the Archives conotrol is able to search for all entries within a given TimeSpan. I would simply be passing the start and end DateTimes without bothering with the user-input textboxes.
While I'm on the subject, is there a way to user the Calendar.OnSelectionChanged event to know whether the selection was changed by the user or by the code? Or is there a better way to handle the user clicking on a day/week/month link? The problem is that changing the selection is how the code highlights the days with entries. Thanks.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.