views:

377

answers:

2

I have an aspx page with three web controls: one to control the List Users page, one to control the Edit Users page, and one to control the Add User page. I have discovered a method for accessing these elements, but it seems to be limited. Here is what I have done:

Protected Sub editUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
        'set selected user from gridview.
        Dim index As Integer = e.NewEditIndex
        Dim userId As Integer = gvListUsers.DataKeys(index).Value
        Session.Item("SelectedUserId") = userId

        'show edit page, hide list and add page.
        With Page.Form.Controls(1)
            .Controls(getControlId("loadAddUser")).Visible = False
            .Controls(getControlId("loadEditUser")).Visible = True
            .Controls(getControlId("loadListUser")).Visible = False
        End With
    End Sub

The getControlId function looks like this:

Public Function getControlId(ByVal control As String) As Integer
        Dim enumer As System.Collections.IEnumerator = Page.Form.Controls.Item(1).Controls.GetEnumerator
        Dim i As Integer

        For i = 0 To (Page.Form.Controls.Item(1).Controls.Count - 1)
            If Page.Form.Controls(1).Controls.Item(i).ID = control Then
                Return i
            End If
        Next
        Return Nothing
    End Function

This works in most cases. However, I am unable to access the "enabled" attribute of these web controls. Why is this, and how might I access that attribute?

Thanks :)

+1  A: 

You could raise events from your UserControls which you subscribe to in the parent ASPX page. In the parent page event action you could enable/disable your controls,

Here's an example of events in UserControls: http://codebetter.com/blogs/brendan.tompkins/archive/2004/10/06/Easily-Raise-Events-From-ASP.NET-ASCX-User-Controls.aspx

Something else to think about: are you getting any benefit from moving this code into usercontrols? Would any of the individual controls be re-usable on their own? Creating tightly coupled controls that rely on each other being present doesn't give you much re-usability of the individual controls.

russau
Thanks for the link. Very interesting. I am using user controls so I can add content to the parent page and swap out simply a piece of this code out and in between the controls.
Chris
A: 

Visible is a property provided by the System.Web.UI.Control class, which is why you can access it directly. Enabled is not a property on this class, so you need to map the control object to a variable of the type of your custom control class if you want to access the Enabled property.

Dim myControl As TheAddUserControl

With Page.Form.Controls(1)    
  myControl = .Controls(getControlId("loadAddUser"))
  myControl.Enabled = False            
  .Controls(getControlId("loadEditUser")).Visible = True            
  .Controls(getControlId("loadListUser")).Visible = False        
End With

To expose an Enabled property in you user control:

Public Property Enabled As Boolean
    Get
     Return (Child1.Enabled And Child2.Enabled And Child3.Enabled)
    End Get
    Set(ByVal value As Boolean)
     Child1.Enabled = value
     Child2.Enabled = value
     Child3.Enabled = value
    End Set
End Poperty
awe
I'm not sure what type TheAddUserControl would be in this. Wouldn't it be of the type System.Web.UI.Control? That was my impression..
Chris
Your user control has a class name. This is the type. Have you added an Enabled property to your user control? If The user control (or the class it inherits from) does not expose any property for Enabled, it is not there. You need to expose a property for it and map it to the Enabled properties on the child elements in the user control.
awe