views:

173

answers:

3

I have a .aspx page that loads three separate .ascx controls to represent adding, editing and listing objects. It currently simply swaps out visibility and enables/disables the controls to switch between pages.

However, when listing objects (in a gridview), we offer the basic "Edit" button beside each one. Clicking on this will, obviously, load up the edit control for that user. This functionality exists inside of the control, rather than the parent page.

Is it possible to modify attributes for the parent based on events occurring in a child control? (For example, change the visibility and enable/disable the other children controls)

A: 

It can be done in the browser, using Javascript or JQuery.

document.getElementById("myID").style.visibility="visible";
document.getElementById("myID").style.visibility="hidden";

Codebehind events are typically queued/delayed until a postback occurs, unless you are using Ajax. If you are using Ajax, you can re-render the part of your form that requires visibility changes.

Robert Harvey
This could be useful! I'll test this out.
Chris
A: 

I don't think I understand your flow. Exactly what happens when you click the edit button in the Grid? Your ItemCommand event fires, and you either find the edit control and make it visible, or you load it and make it visible. Is that correct?

Then, under what circumstances does the edit control want to change the properties of its parent?

John Saunders
When I click the edit button in my grid, it will save the ID of that row, disable and set visibility to false on the list.ascx control, then enable/make visible edit.ascx with the information from the row ID.
Chris
+2  A: 

Yes, you can. We're doing this successfully in our C# project; I think the same thing applies to vb.net.

Each user control has a property called Page which refers to the page that it is hosted under. Create an Interface which exposes the properties you want to change or methods you want to call. Have each page which hosts the user control implement that interface. The implementation should, obviously, impact the correct control(s) on the page.

Then, typecast the Page property to that interface and set your property(ies).
For example (again, this is C#, not sure what the VB.Net equivalent is):

interface IUpdatePage {
    void ChangeButton(String toValue);
}

public class MyPage : Page, IUpdatePage {
    public void ChangeButton(String toValue) {
     // do something interesting
    }
}

public class MyUserControl : UserControl {
    public void UpdateParent() {
     var pageLink = (Page as IUpdatePage);
     pageLink.ChangeButton("some value");
    }
}
Chris Lively
Very interesting, I'll check this out!
Chris
Note, this is obviously all server side. Robert Harvey seems to have a solution for client side.
Chris Lively
I haven't been able to find any elements of the Page class to allow me to accomplish this. Are you by chance, accessing the Parent class belonging to the Page class?
Chris
Page.Form.Controls.Item(1).Controls.Item(AN_INTEGER).ID is giving me some feedback. It's weirdly unpredictable, however. at 0, it's nothing. at 1, it's the first control. at 2, it's nothing. 3 is the second control, etc. Hm.
Chris
No. Your web page should implement the interface. The usercontrol should typecast it's Page property to that interface. The page will determine the control to update.
Chris Lively