Consuming user control custom events Part 107

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to freedom technologies I am venket this is part 107 of asp.net video series in this video we'll discuss about consuming custom events of the calendar user control understanding the importance of checking if the event is now before actually raising the event risk if discussing about this when we are discussing about raising custom events for the calendar user control to get the most out of this video I strongly recommend to watch parts 104 to 106 from the asp.net video series and parts 36 to 39 from the c-sharp video series for your convenience I have included the link to a speed ordinate in c-sharp video series here first let's understand how we actually hook up the click event handler to the click event of the standard asp.net button control so here on this project we have this waveform to where we have this button control and this is our calendar user control that we have created in the previous session so in the code-behind file look at this when I click the button control you know button one let us go click you know this method gets called and then look at that we are printing the selected date within the calendar user control so obviously when I click this look at that as soon as I click the button you know that event handler method which is button 1 let's go click is called and then it's printing the selected date within the calendar user control so the important point to understand is how does the calendar user control knows it has to call this method when the click event happens okay that's specified within the aspx page so we are using this on click attribute to tell you know call this button 1 else go click method when that event happens ok now can't we do this in code absolutely we can do that in code as well look at this if I remove this one you know this declaration from the aspx and then if I run this project and click the button control nothing happens and we understand why is that because the button control doesn't know which method to invoke when the click event happens because you remove that Association now let's look at how to do the same thing within code to do that within code I can use button 1 dot click so when the click event look at the lightning bolt symbol there so click is an event so when the click event happens look at that now dot click + space and then + equals as soon as we do that you know Visual Studio is intelligent enough to you know to know that it's going to use this even handler delegate okay look at that it's using a delegate called even handler - to register a function with name button 1 underscore click OK so button 1 under scope click is the method that needs to be called when this click even happens on this button one control and how are we doing that using event handler delegate okay and look at the signature of this event handler method it has got a white written type and then our two parameters of type object and eventargs okay and then look at the signature of the even handle it delegate itself it has a wide written type and then two parameters of type object and event args so the signature of the delegate matches the signature of the event handler method to which it is pointing okay that's why delegates are actually called type safe function pointers ok type safe because the signature of the delegate matches the signature of the function to which it is pointing to okay and then another important thing to point you know keep in mind since these are function these are called as function pointers why because when this even happens you know this delegate gets invoked and look at this to the constructor of this delegate we're actually passing in the name of the function so this function gets invoked when this date you know when this delegate is invoked so obviously when the click even happens this function this even handler method gets called and and that is through the delegate okay that's where delegates are called function pointers and another point in the previous session we discussed about this as well delegates signature is much similar to that of a function signature so if you look at the signature of the even handler delegate here it's essentially same as that of the even handler method that we have here okay and then obviously with a delegate keyword within the declaration okay now along the same lines you know we have this calendar user control that we have been working with in the previous session and if you remember in the previous session we have created an event for this calendar user control what is that event calendar visibility change the event okay now again in the previous session we discussed that what are events events are variables of type delegates with an event keyword in front of them so calendar visibility change what is this this is a variable of type calendar visibility changed the event handler what is calendar visibility changed even handler that's a delegate okay so that's the delegate that we have created here and look at the signature of this delegate here it has a wide return type and then two parameters of type send object and calendar visibility changed eventargs now I want to hook up an even handler to this event calendar visibility changed event of this calendar user control so let's see how to do that so just like button one control you know the ID of our calendar user control its calendar user control one dot what's the name of the event calendar visibility change look at that that's an event how do we know that let the lightning bolt symbol indicate that and look at this as soon as I select that and then plus space and then type plus equals look at visual studio smart enough to know that calendar visibility changed event is actually you know it's a variable of type calendar visibility changed even handler delegate okay and it's using the delegate look at that when when I plus tab okay that it's automatically generated that generated the delegate declaration and then when I press tab again it generated that method also for me the method signature for me okay so now what are we doing here let's put protected in the method declaration here so that it looks similar to the button click event handler and look at this look at the signature of this event handler method the signature here it it returns wide and two parameters of type object and calendar visibility eventargs okay now if you look at that delegate the signature of this function actually matches the signature of this delegate void return type into parameters okay so it's very similar and what we want to do here another important point to keep in mind here look at this calendar visibility changed eventargs object we are using this object okay so now when I use the table checked I can see that is calendar visible okay so what I basically want to do is I want to print a message on the webform when that event happens and I want to let the user know that whether the calendar is visible or not so I am going to send this message back to the user calendar visible is equal to since that is a boolean property let's go convert that to string and then display that look at that that's it so now when we run this and when we click on the calendar visible you know on the image button to change the visibility of the calendar look at that calendar visible is equal to true because the calendar is visible when I click on the image button again calendar visible is equal to false similarly when I select a date calendar visible is equal to false because the calendar is not visible okay so it's such I mean it's very similar to how we actually hook up even handlers to the events of standard asp.net controls okay now most people feel these events and delegates are complex because there are several moving parts look at this at this point you should you might have understood why we actually created this plus calendar visibility changed even tags in the previous session when we are discussing about raising custom events okay from the calendar user control we have created this class so what's the use of this class because this class is going to contain that even data which we want to retrieve in the even handler okay and what we do just before we are raising the event within the user control we are creating an instance of that object Calendar visibility changed event args and then look at this when the cell when we select a date in the calendar control we turn the visibility of the calendar to false and then we are passing that value to the constructor of this class and then we're passing that you know even data when this event is raised on calendar and then this this object is now received in the event handler method and we are using the property to find out whether if the calendar is visible or not okay so so basically now it should be clear you know about these different moving parts that we are using within events and delegate once you understand that these moving parts it should be fairly straightforward okay so to register a name and handler method we use plus equals and to unregister that we use minus equals look at that Here I am using minus equals and obviously if I run this project now I registered an event handler method and immediately I unregistered that so when I click on the image button nothing happens when the calendar visibility changed event is raised because because there is no even handler method okay because you registered it using plus equals and you unregistered that using minus equals that's the reason okay now another important point to keep in mind is that you know you have to register look at this the even handlers need to be registered before the event is raised now we are doing the registration within the page load event okay and page load event happens before the actual calendar visibility changed event is raised when is the calendar visibility changed event raised this event is raised when we click on this image button okay meaning on the post back post back event happens after page load event okay so by in the page load event we are registering an image handler method and the event is happening after the method is registered so the control knows which method to invoke when that even happens but also remember this page has page pre-render event let me copy this and then within the page pre-render let's register the event handler instead of page load so I am removing that registration code from page load event and moving that to page pre-render okay and let's call this page and when does the page pre-render even happens it happens after the page load even okay now by the time we are registering the even handler to the event the event has already been raised but there was no method listening to that event so obviously you know if we don't have a Nieman handler and nothing happens when that even actually was raised so when I click this nothing happens look at that it doesn't print any message that's because you're doing the registration after the event is actually raised that's why keep in mind the event handlers need to be registered before the event is raised and you can do the registration either and the page load page initialization or page pre initialization now I am doing the registration and the page load event which happens before the event is raised look at that it gets handled okay now we have seen how to you know raise and handily I mean we have to handle events within the code-behind file now can't we do that just like how you know for the button click we were able to do that within the code-behind file I mean within the aspx page as well so for the button I can say on click is equal to which is the method that I want to call button one underscore play along the same lines can't I do this for the calendar user control you can do that as well so we have this on calendar visibility change so when that happens I want to invoke this method okay look at that I am registering the event handler now within the aspx page but if you want to programmatically do that based on some conditions then you can do that in the code as well we have discussed about that so now if I press this it's going to work exactly the same way except that this time the registration is done in the HTML instead of doing that in the code so it works the same way okay now another thing that we need to talk about is um what's the importance of checking of the invitational before actually raising the event okay now so we have an event handler method here registered okay now if you remember in the previous session we use this method protected method to actually raise the image and within the method we are actually checking if the event is not null before raising the email now if I comment this here let's comment that so what happens if I don't check for null now look at this I'm not checking for now and on the webform you know for that even there is an even handler listening okay so now when I press control f5 run the project click the button it works as expected without any issues okay let the page load up so I click that look at that calendar visible is equal to true visible is equal to false fine now let's say there is nobody registered for that event in the sense we don't have any even handler method listening for that event now I run this the page loads are fine but then when I click on the image button the event gets raised okay but then there is nobody listening for that event so basically we get a runtime error look at that I click that button object none reference exception look at this at this point when it's trying to raise the event there's no one listening ok the event is basically now that's why we get this null reference exception that's why it's always a good practice to avoid this runtime exception so showing I mean throwing this runtime exceptions doesn't make any sense if there is no even handler method nothing happens that's it you don't want to throw a null reference exception and how can we avoid that by checking for now that's why you know it's actually very helpful if you use this null check ok so now if I go ahead and run this obviously in spite of not having an even handler method I don't get any runtime error it simply does nothing ok it doesn't show that hello screen of death null reference exception look at that nothing happens that's it but on the other hand if there is an even handle method that gets notified and whatever you specified in that even handle a method to be done that will be done ok in the next video you know we'll discuss about raising another custom event the calendar user control my aim is basically to make sure that these events and hey you know delegates concepts are clear so in this session we have seen how to raise the calendar visibility changed event and how to handle that in the next session will create another you know custom event for the calendar user control probably a calendar date selected or something like that and then we will see how to consume that with these two examples I hope delegates and events and delegates should be fairly simple to understand if you like this video please click the like button you know at the bottom of this video that you can see on this slide you can find resources for HP dot in c-sharp and sequence of interview questions that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 49,315
Rating: undefined out of 5
Keywords: events, delegates, c#, user control, asp.net, consume, handling user control event in web, how to consume user control in asp.net
Id: yV94X953v8s
Channel Id: undefined
Length: 16min 14sec (974 seconds)
Published: Sat Jan 05 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.