-
Aug 17th, 2018, 12:35 PM
#1
Thread Starter
PowerPoster
Confusion accessing subcontrols within UserControl?
This is vexing me big-time. I'm trying to test some things out. The scenario is that I have an ASPX Page that contains a single UserControl. This UC contains 2 dropdownlist controls. So on the ASPX Page, I see the 2 dropdownlist controls. When a value is selected from either dropdownlist, how do I access the selected value from the ASPX page? I'm not sure what event or object declarations need to be made or where for that matter. Hope I explained myself.
Thanks,
-
Aug 17th, 2018, 09:49 PM
#2
Re: Confusion accessing subcontrols within UserControl?
Firstly, let me state that I haven't used Web Forms for a long time and didn't use it much back then anyway. That said, one of the driving principles of Web Forms was that it be as much like Windows Forms as possible. That means that, as far as I'm aware, you should be able to treat your Web Forms controls in pretty much the same way as you would equivalent WinForms controls.
Your DropDownList controls have a SelectedIndexChanged event and that will work in pretty much exactly the same way as the SelectedIndexChanged event of a ComboBox in WinForms. If you want to know about every change use that. Otherwise, you can wait for the page to be posted back. For that, you would handle the Load event and test the IsPostBack property.
As far as accessing the controls and their members are concerned, again, it's pretty much the same as WinForms. Let's say that you had a user control in WinForms that had a ComboBox on it. You would have two choices for accessing members of that ComboBox from outside the user control:
1. Make the child control Public and then access it directly, e.g.
vb.net Code:
Dim selectedIndex = Me.myUserControl.ComboBox1.SelectedIndex
2. Declare a pass-through property in the user control for each member of a child control you want to expose, e.g.
vb.net Code:
Public Property SelectedIndex As Integer Get Return ComboBox1.SelectedIndex End Get Set ComboBox1.SelectedIndex = value End Set End Property
then:
vb.net Code:
Dim selectedIndex = Me.myUserControl.SelectedIndex
Option 2 is more work but is better practice because exposing entire child controls publicly means that any member can be accessed whether it should or not and even the control itself could be replaced.
I haven't tested that to confirm everything but I'm fairly certain that it's correct and, if so, it's exactly the same as Winforms, which is the point of Web Forms.
May I just say that I started out as a Windows Forms developer but I always found Web Forms to feel like a hack and quite unnatural. As soon as Microsoft introduced the MVC Framework for ASP.NET, I started using that and found it far more natural for the web. MVC is far more mature now too so, if you have the choice, I'd suggest using that for ASP.NET applications if you can. There will be a bit of a learning curve but it seems that you're finding that in Web Forms too, despite the fact that it's supposed to feel like WinForms. Among the other advantages of MVC is that you can create a web application and a web service is almost exactly the same way, because Web API is basically MVC for web services. It just lacks the V (view) part but the C (controller) and M (model) work in exactly the same way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|