Microsoft ASP Net 3.5 Step By Step (phần 5) pdf

30 338 0
Microsoft ASP Net 3.5 Step By Step (phần 5) pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 4 Custom Rendered Controls 91 4. Build the project by selecting Build, Build Solution from the main menu. 5. Add the PalindromeCheckerRenderedControl to the toolbox if it’s not already there. Visual Studio should add the PalindromeCheckerRenderedControl to the Toolbox. If not, you can add it manually. Click the right mouse button on the toolbox and select Choose Item. Use the Browse button to fi nd the CustomControlLib.DLL assembly and select it. Visual Studio will load the new control in the toolbox. 6. Add a page to use the palindrome checker control. Add a new Web Form to the ControlORama project and name it UsePalindromeCheckerControls.aspx. Drag the PalindromeCheckerRenderedControl and drop it on the page. Add a TextBox and a but- ton so you can add a palindrome to the control and check it. 7. Add a handler for the button. Double-click on the button. Visual Studio will add a han- dler to the page. In the handler, set the PalindromeCheckerRenderedControl’s text prop- erty to the TextBox.Text property. public partial class UsePalindromeCheckerControls : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { this.PalindromeCheckerRenderedControl1.Text = this.TextBox1.Text; } } 8. Run the page and test for a palindrome. Palindromes should appear in blue and non- palindromes in red. 92 Part I Fundamentals Controls and Events The PalindromeCheckerRenderedControl shows how to render control content differently de- pending on the state of the Text property. While that’s a very useful thing in itself, it’s often helpful to also alert the host page to the fact that a palindrome was found. You can do this by exposing an event from the control. Most of ASP.NET’s standard server-side controls already support events. You’ve already seen how the Button control sends an event to the host page when it is clicked. You can actually do this type of thing with any control. Let’s add a PalindromeFound event to the PalindromeCheckerRenderedControl. Adding a PalindromeFound event 1. Open the PalindromeCheckerRenderedControl.cs fi le. To add a PalindromeFound event, type in the following line: public class PalindromeCheckerRenderedControl : WebControl { public event EventHandler PalindromeFound; // Other palindrome control code goes here } Chapter 4 Custom Rendered Controls 93 2. Once hosts have subscribed to the event, they’ll want to know when the event fi res. To do this, fi re an event on detecting a palindrome. The best place to do this is within the Text property’s setter. Add the boldfaced lines of code to the palindrome’s Text prop- erty and rebuild the project: [Bindable(true)] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public string Text { get { string s = (string)ViewState["Text"]; return ((s == null) ? String.Empty : s); } set { ViewState["Text"] = value; if (this.CheckForPalindrome()) { if (PalindromeFound != null) { PalindromeFound(this, EventArgs.Empty); } } } } Notice that the code generated by Visual Studio 2008 stores the property in the con- trol’s ViewState. That way, the property retains its value between posts. We’ll examine ViewState more closely later in this chapter. 3. Now wire the event in the host page. Remove the current instance of the PalindromeCheckerRenderedControl from the page and drop a new instance on the page. This will refresh the CustomControlLib.DLL assembly so the changes (the new event) will appear in Visual Studio. Select the PalindromeCheckerRenderedControl on the page and click the Events button (the little lightning bolt) in the property page in Visual Studio. Double-click on the text box next to the PalindromeFound event. Visual Studio will create an event handler for you. 94 Part I Fundamentals 4. Respond to the PalindromeFound event. The example here simply prints some text out to the browser using Response.Write. public partial class UsePalindromeCheckerControls : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { this.PalindromeCheckerRenderedControl1.Text = this.TextBox1.Text; } protected void PalindromeCheckerControl1_PalindromeFound( object sender, EventArgs e) { Response.Write("The page detected a PalindromeFound event"); } } Chapter 4 Custom Rendered Controls 95 Run the page. You should see something like the following when you type a palindrome: Now that the control renders palindromes correctly and has an event, let’s take a closer look at the parameter passed in during the call to Render: HtmlTextWriter. HtmlTextWriter and Controls Go back and review the control’s RenderContents method for a minute. Notice that the RenderContents method places literal font tags to change the color of the palindrome text. While this is certainly effective, this technique has a couple of downsides. For example, HTML is defi ned by multiple standards. That is, browsers running both HTML version 3.2 and ver- sion 4.0 occur in nature. Certain HTML elements have changed between version 3.2 and version 4.0. If you render all your HTML directly expecting requests from a certain kind of browser, your users may be taken by surprise if they browse to your page with a new browser that interprets HTML differently. 96 Part I Fundamentals Note The .NET framework includes multiple versions of the HtmlTextWriter class: Html32TextWriter, HtmlTextWriter, XhtmlTextWriter, and ChtmlTextWriter. When a request comes from a browser, it always includes some header information indicating what kind of browser made the request. Most browsers these days are capable of interpreting the current version of HTML. In this case, ASP.NET passes in a normal HtmlTextWriter into the RenderControl method. However, if you happen to get a request from a lesser browser that understands only HTML 3.2, ASP.NET passes in an Html32TextWriter. The classes are similar as far as their use and may be in- terchanged. Html32TextWriter emits certain tags (such as table tags) in HTML 3.2 format, while HtmlTextWriter emits the same tags in HTML4.0 format. Information within machine.confi g and the browser capabilities confi guration help ASP.NET fi gure out what kind of HtmlTextWriter to use. The browser capability information deduced by the ASP.NET runtime may be used for more than simply selecting the correct HtmlTextWriter. The Request property (available as part of the HttpContext and the Page) includes a reference to the Browser object. This object includes a number of fl ags indicating various pieces of information, such as the type of browser making the request, whether the browser supports scripting, and the name of the platform the browser is running on. This information comes down as part of the headers included with each request. The ASP.NET runtime runs the headers against some well-known regular expressions within the con- fi guration fi les to fi gure out the capabilities. For example, here’s a short listing illustrating how to fi gure out if the browser making the request supports Frames: public class TestForFramesControl : Control { protected override void RenderContents(HtmlTextWriter output) { if (Page.Request.Browser.Frames) { output.Write( "This browser supports Frames"); } else { output.Write("No Frames here"); } } } To get a feel for using the more advanced capabilities of HtmlTextWriter, replace the hard- coded font tags in the RenderContents method of the PalindromeCheckerRenderedControl with code that uses the HtmlTextWriter facilities. Use the HtmlTextWriter 1. Open the PalindromeCheckerRenderedControl.cs fi le. 2. Update the RenderContents method to use the HtmlTextWriter methods. Use HtmlTextWriter.RenderBeginTag to start a font tag and a bold tag. Use HtmlTextWriter .AddStyleAttribute to change the color of the font to blue. protected override void RenderContents(HtmlTextWriter output) { if (this.CheckForPalindrome()) Chapter 4 Custom Rendered Controls 97 { output.Write("This is a palindrome: <br/>"); output.RenderBeginTag(HtmlTextWriterTag.Font); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue"); output.RenderBeginTag(HtmlTextWriterTag.B); output.Write(Text); output.RenderEndTag(); // bold output.RenderEndTag(); // font } else { output.Write("This is a palindrome: <br/>"); output.RenderBeginTag(HtmlTextWriterTag.Font); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue"); output.RenderBeginTag(HtmlTextWriterTag.B); output.Write(Text); output.RenderEndTag(); // boldl output.RenderEndTag(); // font } } The HtmlTextWriter class and the enumerations include support to hide all the oddities of switching between HTML 3.2 and 4.0. Listing 4-5 shows how a table would be rendered using an HTML 4.0–compliant response. Listing 4-6 shows how a table would be rendered using an HTML 3.2–compliant response. LISTING 4-5 HTML 4.0 Rendered Control <br /> <br /> This is a palindrome: <br> <b><font>Do geese see god?</font></b><br> <table width="50%" border="1" style="color:blue;"> <tr> <td align="left" style="font-size:medium;color:blue;"> A man, a plan, a canal, panama.</td> </tr> <tr> <td align="left" style="font-size:medium;color:blue;"> Do geese see god?</td> </tr> LISTING 4-6 HTML 3.2 Rendered Control <br /> <br /> This is a palindrome: <br> <b><font>Do geese see god?</font></b><br> <table width="50%" border="1"> <tr> <td align="left"> <font color="blue" size="4">A man, a plan, a canal, panama.</font> </td> </tr> <tr> <td align="left"><font color="blue" size="4">Do geese see god?</font> </td> </tr> 98 Part I Fundamentals Controls and ViewState Before leaving rendered controls, let’s take a look at the issue of control state. If you go back to some of the classic ASP examples from earlier chapters, you may notice something dis- concerting about the way some of the controls rendered after posting back. After you select something in the combo box and make a round-trip to the server, by the time the response gets back, the controls (especially selection controls) have lost their state. Recall that the basic Web programming model is all about making snapshots of the server’s state and displaying them using a browser. We’re essentially trying to perform stateful user interface (UI) develop- ment over a disconnected protocol. ASP.NET server-side controls include a facility for holding onto a page’s visual state—it’s a property in the Page named ViewState, and you can easily access it any time you need. ViewState is a dictionary (a name-value collection) that stores any serializable object. Most ASP.NET server-side controls manage their visual state by storing and retrieving items in the ViewState. For example, a selection control might maintain the index of the selected item between posts so that the control knows which item has its selected attribute assigned. The entire state of a page is encoded in a hidden fi eld between posts. For example, if you browse to an ASPX page and view the source code coming from the server, you’ll see the ViewState come through as a BASE 64–encoded byte stream. To get a feel for how ViewState works, add some code to keep track of the palindromes that have been viewed through the control. Using ViewState 1. Open the PalindromeCheckerRenderedControl.cs fi le. 2. Add System.Collections to the list of using directives. using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections 3. Add an ArrayList to the control to hold the viewed palindromes. Update the Text prop- erty’s setter to store text in the view state if the text is a palindrome. public class PalindromeCheckerRenderedControl : WebControl { public event EventHandler PalindromeFound; // public event ArrayList alPalindromes = new ArrayList(); [Bindable(true)] Chapter 4 Custom Rendered Controls 99 [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public string Text { get { String s = (String)ViewState["Text"]; return ((s == null) ? String.Empty : s); } set { ViewState["Text"] = value; string text = value; this.alPalindromes = (ArrayList)this.ViewState["palindromes"]; if (this.alPalindromes == null) { this.alPalindromes = new ArrayList(); } if (this.CheckForPalindrome()) { if (PalindromeFound != null) { PalindromeFound(this, EventArgs.Empty); } alPalindromes.Add(text); } ViewState.Add("palindromes", alPalindromes); } } } 4. Add a method to render the palindrome collection as a table and update the RenderContents method to render the viewed palindromes. protected void RenderPalindromesInTable(HtmlTextWriter output) { output.AddAttribute(HtmlTextWriterAttribute.Width, "50%"); output.AddAttribute(HtmlTextWriterAttribute.Border, "1"); output.RenderBeginTag(HtmlTextWriterTag.Table); //<table> foreach (string s in this.alPalindromes) { output.RenderBeginTag(HtmlTextWriterTag.Tr); // <tr> output.AddAttribute(HtmlTextWriterAttribute.Align, "left"); output.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "medium"); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue"); output.RenderBeginTag(HtmlTextWriterTag.Td); // <td> output.Write(s); output.RenderEndTag(); // </td> output.RenderEndTag(); // </tr> } output.RenderEndTag(); // </table> } 100 Part I Fundamentals protected override void RenderContents (HtmlTextWriter output) { if (this.CheckForPalindrome()) { output.Write("This is a palindrome: <br>"); output.RenderBeginTag(HtmlTextWriterTag.Font); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue"); output.RenderBeginTag(HtmlTextWriterTag.B); output.Write(Text); output.RenderEndTag(); // bold output.RenderEndTag(); // font } else { output.Write("This is NOT a palindrome: <br>"); output.RenderBeginTag(HtmlTextWriterTag.Font); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "red"); output.RenderBeginTag(HtmlTextWriterTag.B); output.Write(Text); output.RenderEndTag(); // bold output.RenderEndTag(); // font } output.Write("<br>"); RenderPalindromesInTable(output); } 5. Build and run the application. When you surf to the page holding the palindrome checker, you should see the previously found palindromes appearing in the table: [...]... not already showing by selecting View, Toolbox from the main menu Click the right mouse button anywhere in the Toolbox Select Choose Items from the local menu Choose a control from the list OR Browse to the assembly containing the control Tell ASP. NET to assign unique IDs for the child controls within either type of composite control Derive the binary composite control from ASP. NET s CompositeControl... selection controls that may need to track collections of items Even though input buttons and selection controls are easy to describe in HTML, we’ve seen that ASP. NET already includes server-side control classes that render the correct tags The standard ASP. NET controls greatly simplify user interface (UI) programming for Web forms Composite controls take advantage of these server-side controls that have already... at some of the other controls available within ASP. NET Chapter 5 Quick Reference To Do This Create a binary control composed of other server-side controls that lives in its own assembly Override the CreateChildControls method Add controls to a binary composite control Derive a class from System.Web.UI.Control Visual Studio includes a project type, ASP. NET Server Control, that fits the bill Instantiate... overuse can bog down a site’s performance Summary ASP. NET s Page infrastructure is set up so that each page is broken down into smaller components (server-side controls) that are responsible for rendering a small amount of HTML into the page’s output stream After reading this chapter, you probably have a good idea as to how some of the standard ASP. NET controls are rendered Button controls render as... don’t need to keep writing the same HTML over and over Note Beginning with version 2.0, ASP. NET includes a set of login composite controls so you don’t need to write new ones from scratch However, they are mentioned here because they represent an excellent illustration for the power of composite controls Let’s begin by looking at custom composite controls Custom Composite Controls In Chapter 4, we saw... provided ASP. NET control (such as the element), you need to package it in a server-side control The job of a LiteralControl is to take the contents (the Text property) and simply render it to the outgoing stream The second thing to notice is how the event handler is hooked to the Button using a delegate When you use Visual Studio’s designer support, you can usually wire event handlers up by clicking... over the rendering process Override the RenderContents method Derive a class from System.Web.UI.Control Visual Studio includes a project type, ASP. NET ServerControl, that fits the bill Add a custom control to the toolbox Show the toolbox if it’s not already showing by selecting View, Toolbox from the main menu Click the right mouse button anywhere in the toolbox Select Choose Items from the local menu Choose... an input tag with a type of “submit.” TextBox controls render as an input tag with a type of “text.” You can actually see how each of the controls in a page renders by viewing the HTML that comes back to the browser Of course, because ASP. NET s Page infrastructure is set up this way, it leaves the door open for custom User controls In this chapter, we looked at rendered custom controls Custom controls... browser The factor distinguishing this kind of control most is that these controls override the RenderContents method Remember, the System.Web.UI.Page class manages a list of serverside controls When ASP. NET asks the whole page to render, it goes to each control on the page and asks it to render In the case of a rendering control, the control simply pushes some text into the stream bound for the browser... CustomControlLib project in the Solution Explorer Click the right mouse button on the project node and select Add, New Item Create a new class and name the source file PalindromeCheckerCompositeControl.cs Use the ASP. NET Server Control template (as you did with the PalindromeCheckerRenderedC ontrol from Chapter 4) Chapter 5 Composite Controls 105 2 After Visual Studio creates the code, do the following: Edit the . this case, ASP. NET passes in a normal HtmlTextWriter into the RenderControl method. However, if you happen to get a request from a lesser browser that understands only HTML 3. 2, ASP. NET passes. browser capabilities confi guration help ASP. NET fi gure out what kind of HtmlTextWriter to use. The browser capability information deduced by the ASP. NET runtime may be used for more than simply. are easy to describe in HTML, we’ve seen that ASP. NET already includes server-side control classes that render the correct tags. The standard ASP. NET controls greatly simplify user interface

Ngày đăng: 07/07/2014, 06:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan