I am looking for some insight on the best way to update the View of a MultiView via a button's click event located in a Child's User Control. Please see the following example Code:
Child ASCX UserControl
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
Child ASCX.vb UserControl
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
End Sub
How would I call the parent Multiview in the User Control's button click event? I was thinking somewhere along the lines of the following but need assistance please. Thanks
The proper/appropriate way to handle this is via a custom event. However, if it's something simple that you wish to do to the formview, then you might use findcontrol on the NamingContainer property if the usercontrol (cast to page).
Thanks for the response Metal, I have been trolling the net looking at how best to accomplish this using a custom event, however, I am finding this way somewhat difficult to implement. I am very keen to use the proper/appropriate way, is there any tutorials/sites
that you recommend that I can look at that has something similiar to what I am trying to do?
I've done this on several occasions but I always forget how to do it exactly. So I always refer to this page: http://msdn.microsoft.com/en-us/library/w369ty8x.aspx. So the idea is that you create a custom event in the usercontrol (using a process similar
to the posted link) and subscribe to this event in the parent/container page. You raise this event in the user control and so it calls whatever handler you're hooked up in the parent page. I hope this helps.
Metal, thanks again. It's been a long day but I believe I am nearly there. This is what I have been working on since your previous post.
This is what I added to my User Control:
Public Delegate Sub MyEventHandler(ByVal sender As Object, e As EventArgs)
Public Event MyEvent As MyEventHandler
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
RaiseEvent MyEvent(sender, e)
End Sub
Then in my ASPX page I added:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
AddHandler ucAddMenu.MyEvent, AddressOf Me.AddMenuControlBtnClick
End If
End Sub
Public Sub AddMenuControlBtnClick(ByVal sender As Object, ByVal e As EventArgs)
MultiView1.SetActiveView(View1)
End Sub
I know that the button is working as I have extra code within the button click event that is writing data to a database, just that the multiview is not changing back to View1?
Does any of this look out of sorts? Have I missed something out?
I'm sorry to say this all looks foreign to me as I know very little VB.NET. But I'll try to advise...
Try removing the If (Not IsPostBack) so the event is hooked up on every postback. Or alternatively, hook up the event in the HTML markup directly; you would use
something like: OnMyEvent="AddMenuControlBtnClick" and remove the code in page_load for hooking up the event.
Finally managed it, here is the code as reference:
UserControl Code Behind
Public Event MyEvent As EventHandler
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
RaiseEvent MyEvent(sender, e)
End Try
Parent Page Code Behind
Private Sub ucExample_MyEvent(sender As Object, e As System.EventArgs) Handles ucExample.MyEvent
MultiView1.SetActiveView(View1)
End Sub
Over 10 hours to figure out just this.. arghh! Thanks for the help Metal
morrisandy
Member
30 Points
46 Posts
Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 19, 2012 04:41 PM|LINK
Afternoon Everybody,
I am looking for some insight on the best way to update the View of a MultiView via a button's click event located in a Child's User Control. Please see the following example Code:
Parent ASPX Page <asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server"> <asp:View ID="View1" runat="server"> <asp:Label ID="lblExample" Text="View 1" runat="server"> </asp:Label> </asp:View> <asp:View ID="View2" runat="server"> <uc:UserCtrl ID="ucExample" runat="server" /> </asp:View> </asp:MultiView>How would I call the parent Multiview in the User Control's button click event? I was thinking somewhere along the lines of the following but need assistance please. Thanks
Dim MultiView1 As MultiView
MultiView1 = CType(Me.Parent.FindControl("View1"), MultiView)
MetalAsp.Net
All-Star
112051 Points
18235 Posts
Moderator
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 19, 2012 05:01 PM|LINK
morrisandy
Member
30 Points
46 Posts
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 19, 2012 06:41 PM|LINK
Thanks for the response Metal, I have been trolling the net looking at how best to accomplish this using a custom event, however, I am finding this way somewhat difficult to implement. I am very keen to use the proper/appropriate way, is there any tutorials/sites that you recommend that I can look at that has something similiar to what I am trying to do?
Thanks
MetalAsp.Net
All-Star
112051 Points
18235 Posts
Moderator
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 19, 2012 06:53 PM|LINK
I've done this on several occasions but I always forget how to do it exactly. So I always refer to this page: http://msdn.microsoft.com/en-us/library/w369ty8x.aspx. So the idea is that you create a custom event in the usercontrol (using a process similar to the posted link) and subscribe to this event in the parent/container page. You raise this event in the user control and so it calls whatever handler you're hooked up in the parent page. I hope this helps.
morrisandy
Member
30 Points
46 Posts
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 20, 2012 01:51 AM|LINK
Metal, thanks again. It's been a long day but I believe I am nearly there. This is what I have been working on since your previous post.
This is what I added to my User Control:
Then in my ASPX page I added:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If (Not Page.IsPostBack) Then AddHandler ucAddMenu.MyEvent, AddressOf Me.AddMenuControlBtnClick End If End Sub Public Sub AddMenuControlBtnClick(ByVal sender As Object, ByVal e As EventArgs) MultiView1.SetActiveView(View1) End SubI know that the button is working as I have extra code within the button click event that is writing data to a database, just that the multiview is not changing back to View1?
Does any of this look out of sorts? Have I missed something out?
MetalAsp.Net
All-Star
112051 Points
18235 Posts
Moderator
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 20, 2012 04:52 AM|LINK
I'm sorry to say this all looks foreign to me as I know very little VB.NET. But I'll try to advise...
Try removing the If (Not IsPostBack) so the event is hooked up on every postback. Or alternatively, hook up the event in the HTML markup directly; you would use something like: OnMyEvent="AddMenuControlBtnClick" and remove the code in page_load for hooking up the event.
morrisandy
Member
30 Points
46 Posts
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Feb 20, 2012 03:16 PM|LINK
Finally managed it, here is the code as reference:
UserControl Code Behind
Parent Page Code Behind
Over 10 hours to figure out just this.. arghh! Thanks for the help Metal
bing25
Member
106 Points
163 Posts
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Mar 05, 2012 11:06 PM|LINK
Thank you morrisandy
This post was really helpful
morrisandy
Member
30 Points
46 Posts
Re: Updating MultiView in Parent Page from User Control's button Click Event. VB.NET
Mar 06, 2012 01:38 AM|LINK
Glad it helped bing