Now that we have discussed overriding methods we are ready to create a generic "New..." button for "quick entry" forms. The way to accomplish this is to create your own button control. This sounds more difficult than it actually is. A custom control is a class that derives from System.Web.UI.WebControls.WebControl. When you derive the class from WebControl you first have to call the base class's constructor and tell it what type of control you are creating. (Remember, a constructor is a function that executes when we create an instance of a class.) In this case you are going to tell the base class that we are creating a button control (System.Web.UI .HtmlTextWriterTag.Button). Next we have to override two functions in WebControl. The first function is called AddAttributesToRender. In this function we're going to tell ASP.NET that whenever we generate the HTML for our control we want the tag for our button to have the onClick attribute to invoke the client-side script we wrote in Page_Load. In other words, when the user clicks the button, the code should invoke the client-side script. The second function we have to override is the RenderContents function. In this function we tell ASP.NET how to generate the HTML for our control. We don't have to worry about generating the HTML to make our control a button because we do that in the constructor. But by default our button will be blank, so in the RenderContents function we'll add code to set the text for the button to "New...". To create a generic button class:
Tips
|