GoogleControl |
|
Concept | Presentation | Install | Use | Internals |
GoogleControl internalsForewordGoogleControl implements the three forms of PageBox control described in Control concept:
GoogleControl is nothing more than an example of implementation relevant in an ASP.NET environment. It primarily aims to present tricks solving issues related to control development. GoogleSearchService.csThe three versions use the proxy included in the Google developer kit, GoogleSearchService.cs that you can find at http://www.google.com/apis/. All versions call the GoogleSearchService’s doGoogleSearch method in their bind method. bindThe bind method builds an ArrayList of ResultItem elements and returns the number of entries found. When the license (retrieved either from GoogleControl.xml or from the google:control attribute) has the value offline, bind builds a fake list. Otherwise it builds the list from the GoogleSearchResult object returned by doGoogleSearch. ResultItemThe ResultItem class contains the string fields returned by Google:
It is self-explanatory. Check the documentation in the Google developer kit for more explanations. The ResultItem ArrayList is bound to the result Datagrid. For that usage ResultItem implements three properties, URL, Title and Key. URL returns the url field and Title returns the title field. Key returns all fields separated by a | to simplify the parsing by dgResult_Select. GoogleControl\Global.asax.csGlobal.asax.cs is used both for GoogleInclude (Include control) and for GoogleForm (HTTP control). Global.asax.cs implements an initialize method. InitializeThe function of initialize is to read and parse GoogleControl.xml that contains:
In case of HTTP control (GoogleForm), Global.asax needs to manage up to one GoogleControl.xml file per client directory. Therefore Global.asax maintains a Hashtable of RefererItem objects named pages. A RefererItem contains a memory image of a GoogleControl.xml file. The pages Hashtable uses the URL of the client directory as key. initialize takes three parameters:
When initialize is called by GoogleInclude, the client page, GoogleInclude and its Global.asax are in the same directory that also contains GoogleControl.xml. initialize just need to read the local GoogleControl.xml. When it is called by GoogleForm, initialize first tries to retrieve the client GoogleControl.xml with an HTTP GET. When the client directory has no GoogleControl.xml, initialize tries to retrieve a local GoogleControl.xml. Then initialize parses GoogleControl.xml and populates a RefererItem object. Eventually initialize inserts this RefererItem object in the pages Hashtable. GoogleControl\GoogleInclude.aspx.csGoogleInclude.ascx.cs implements the Include control. Beside the bind method presented in the GoogleSearchService.cs section, GoogleInclude.aspx.cs implements six methods. Page_LoadPage_Load first calls the initialize method of Global.asax. Then it sets the min width (using the resize method), the CSS classes of the title and copyright from the RefererItem data. getFormgetForm is a service method that retrieves the GoogleInclude form. resizeresize first retrieves the control width from the RefererItem object. GoogleInclude width is the width of a table. Therefore resize enumerates the components of the GoogleInclude form and sets the width in the <table> tag. OnSearchOnSearch is invoked when the user clicks on the Search button. It clears the detail DataGrid using an unbindDetail method. Then it calls the bind method to build the ArrayList of ResultItem. In case of success, it binds the result DataGrid to the ArrayList and it sets the control width to MaxWidth with the resize method. dgResult_SelectdgResult_Select is invoked when the user clicks on a Sel button of the result DataGrid. It parsed the Key of the selected row and populates the detail DataGrid. OnClearOnClear is invoked when the user clicks on the Clear button. It clears the detail DataGrid using the unbindDetail method. It also clears the result DataGrid and sets the control width to MinWidth with the resize method. GoogleControl\GoogleForm.aspx.csGoogleForm.ascx.cs implements the HTTP control for server pages. Beside the bind method presented in the GoogleSearchService.cs section, GoogleForm.aspx.cs implements six methods. Page_LoadPage_Load calls the getReferer method to retrieve the URL of the client page. Page_Load then calls the initialize method of Global.asax. Next it sets the min width (using the resize method), the CSS classes of the title and copyright from the RefererItem data. getReferergetReferer retrieves the URL of the client directory
getReferer stores the URL of the client directory in the session object. getFormgetForm is a service method that retrieves the GoogleForm form. resizeresize first retrieves the control width from the RefererItem object. GoogleForm width is the width of a table. Therefore resize enumerates the components of the GoogleForm form and sets the width in the <table> tag. OnSearchOnSearch is invoked when the user clicks on the Search button. It clears the detail DataGrid using an unbindDetail method. Then it calls the bind method to build the ArrayList of ResultItem. In case of success, it binds the result DataGrid to the ArrayList and it sets the control width to MaxWidth with the resize method. dgResult_SelectdgResult_Select is invoked when the user clicks on a Sel button of the result DataGrid. It parsed the Key of the selected row and populates the detail DataGrid. OnClearOnClear is invoked when the user clicks on the Clear button. It clears the detail DataGrid using the unbindDetail method. It also clears the result DataGrid and sets the control width to MinWidth with the resize method. GoogleControl\GoogleForm2.aspxGoogleForm2.aspx implements the HTTP control for browsers. GoogleForm2 is designed to be used with frames and IFrames. The only differences between GoogleForm2.aspx and GoogleForm.aspx are
Here is how it works: Cpy is the ID of the copyright field that sits at the bottom of the control. The offsetTop of this element is about the height of the control. Table1 is the ID of the table that surrounds the control. The width of this element is about the width of the control. GoogleControl is the ID of the IFrame that includes the control in the client page. The OnLoad function retrieves this element and sets its height to the offsetTop of Cpy plus something and its height to the width of Table1 plus something. Note: This code works with IE 5.5 and above and with Gecko engines (Mozilla and Netscape 6). GoogleControl\ControlCss.aspx.csControlCss.aspx solves a simple problem: The second page of GoogleForm.aspx.cs is displayed standalone. Therefore its HTML <head> is read. It contains:
ControlCss.aspx returns the CSS defined in the GoogleControl.xml of the client page and therefore allows setting the actual CSS dynamically. ControlCss.aspx.cs implements an include method called from ControlCss.aspx that could hardly be shorter:
includeThe include implementation relies on the fact that the CSS is queried in the same session as GoogleForm.aspx. GoogleForm.aspx.cs stores the client page URL in the session (see the GoogleForm.aspx.cs’ getReferer method for more detail) and include retrieves the client page URL from the session. Then include uses the URL to retrieve the relevant RefererItem. Next include makes an HTTP GET request to get the CSS pointed by RefererItem. Eventually include writes the stream on the ControlCss response. GoogleControl2\GoogleControl.ascx.csGoogleControl.ascx.cs implements the ASP.NET control. Beside the bind method presented in the GoogleSearchService.cs section, GoogleControl.ascx.cs implements four methods and properties mapped on attributes of the google:control element. Properties
Page_LoadExcept in case of PostBack, Page_Load sets the CSS class of the page control and sets the width of the panel to MinWidth OnSearchOnSearch is invoked when the user clicks on the Search button. It clears the detail DataGrid using an unbindDetail method. Then it calls the bind method to build the ArrayList of ResultItem. In case of success, it binds the result DataGrid to the ArrayList and it sets the panel width to MaxWidth. dgResult_SelectdgResult_Select is invoked when the user clicks on a Sel button of the result DataGrid. It parsed the Key of the selected row and populates the detail DataGrid. OnClearOnClear is invoked when the user clicks on the Clear button. It clears the detail DataGrid using the unbindDetail method. It also clears the result DataGrid and sets the panel width to MinWidth.
Contact:support@pagebox.net |
|