Creating reusable should be on every developers agenda, and one way to do that in ASP.NET Web Forms is to create your own user controls, which can be reused across pages.
- [Instructor] Now that we are done talking about how we can create maintainable code, let's see how we can create reusable code. And the whole point of this discussion is actually to make sure that we understand what choices do we have when we start creating new controls or how we can reuse some of the code that we are writing. And for this purpose I have created a simple Contact us form inside our contact page. And in there I can put actually a name of the user and then some text like "Hello World". And then I'll press send and this will actually show me that user Tibi said Hello World. So it's very simple contact form information. Let me show you how the code looks like for these one. So, I have added on the existing contact page so this is the code how it looked before up to this point. And what I added was this contact as part in here. So what we have, we have a table we have a name and then we have a text box containing the txtName and then we have another one called Message and then the text box called txtMessage. Then we have a button and a button has the sent text. When you press that one, the btnSend_Click would be invoked. And we have as well Label called lblOutput. So the code itself it's a simple as it gets. If you invoke the btnSend_Click meaning that you click on the button Send, then we are changing the text of the output to user said by using string.Format method. So it's very simple, this is a very simple way to do that. Now one of the things that I want to do I realize that this name and message can be reused. So one of the things that I want to reuse it for instance is later on if I want to edit either the name or the message or something I just don't want to recreate this control over and over again. Or those two controls to add them on a page so I just use this one as one single control. So the simplest way for us to do that is to actually go back into our solution explorer and then if I right click on the project, I can say add and then I can add a new item and item would be a user control. So if we look at the web, we have on the web form, Web Forms User Control. And this one can have a name. So the name that I want to use for this one would be ContactForm.ascx. This is a simple control that I am creating. When I create the control, I get almost the same thing as I got with a new page. So instead of aspx I get an ascx. And instead of the page directive I have a control directive. But everything else more or less it's the same. So exactly the same discussion as we had before applies here. So now if I want to make this one a control what I have to do is take the contact aspx from here take the whole content of the table that is here. Control X. And then move it inside the control. And once I'm done with that, I have more or less similar control that I can start reusing inside my forms.
- Working with ASP.NET Web Forms controls
- Creating ASP.NET Web Forms pages
- Designing maintainable code
- Debugging ASP.NET Web Forms applications
- Creating reusable code
- Working with SQL Server
- Presenting datasets in ASP.NET Web Forms apps