Tutorials to .com

Tutorials to .com » Asp » Base » Advanced Programming ASP 3.0 (25)

Advanced Programming ASP 3.0 (25)

Print View , by: iSee ,Total views: 15 ,Word Count: 3717 ,Date: Sat, 25 Apr 2009 Time: 9:48 PM

6.2.4 Counters Component
Counters can be used to create components, storage, and retrieval of increasing the value of each counter. Do not put this chapter behind it and will be introduced to confuse the Page Counter Component, Counters components can be used to support any type of statistical data.
A counter containing an integer value, through the methods Counters computing components. Set counter to use the designated method of setting value, the method of retrieval Counter Get the value, use the Increment method counter value plus 1, use the Remove method to remove a counter. The value of all the counters are stored in a text file Counters.txt can be Counters.dll components in the directory where to find the file.
1. Counters component members
Counters provided components for the maintenance of each counter value of the four components, such as shown in Table 6-4:
Table 6-4 Counters and components description

Get (counter_name)
Return the current value of the designated counter, if the counter had not created the moral to create and set to 0, the return value of 0

Increment (counter_name)
Increase the current value of the designated counter, if the counter had not created, first of all to create and set to 1

Remove (counter_name)
Delete the specified counter

Ser (counter_name, value)
The value of the specified counter value set to the parameters provided by the integer value, if the counter had not created the first to create and set to specified value

2. Counters components used
Counters.txt document as a result of only one instance can access all the components of the copy. Therefore, we should only create a single instance of the Counters component and on the Web site so that all pages are available to achieve this common method is in the root directory of the default Web site under the global.asa file to create an application Examples of procedures.
Using the following procedure:
<! - Declare instance of the asp Counters component with application-level scope
<OBJECT ID = "objCounters" RUNAT = "Server" SCOPE = "Application"
PROGID = "MSWC.Counters">
Counters components can be used to complete the tasks that need to create a new counter. In the following procedure, given a choice of three survey questions, and answered each of the number of choices for the statistics, when the user submit the form contains three options will be transferred to this page. The assumption that option by clicking on the SUBMIT button cmdYes, cmdNo and cmdMaybe to choose, and its corresponding value is "yes", "No" and "may."
<% 'In vbscript "
If Request.Form ( "cmdYes") = "Yes" Then objCounter.Increment ( "Response_Yes")
If Request.Form ( "cmdNo") = "No" Then objCounter.Increment ( "Response_No")
If Request.Form ( "cmdMaybe") = "Maybe" Then
objCounter.Increment ( "Response_Maybe")
If this is the first response received for a specified, the program will create a new counter and automatically initialized to 1.
Scope in the use of counters there is no restriction, because the Counters object created in the global.asa file, this means that in a virtual application or Web site to create any one page is available, so this "counter survey" can be used in the application of procedures of any page, keep in mind a single object can provide the necessary Counters many independent counters, do not need to create a lot of Counters object instance.
In front page of the Ad Rotator component example, studied how to use each storage component Counters advertisers clicks, the page can also be used in the Get method Counters component displays the current value.
Wrox Press: <B> <% = objCounters.Get ( "wrox")%> </ B> <BR>
Stonebroom: <B> <% = objCounters.Get ( "stonebroom")%> </ B> <BR>
Xtras: <B> <% = objCounters.Get ( "xtras")%> </ B> <BR>
ComponentSource: <B> <% = objCounters.Get ( "compsrc")%> </ B> <BR>
Four CDs: <B> <% = objCounters.Get ( "fourcds")%> </ B> <BR>
Lunar: <B> <% = objCounters.Get ( "lunar")%> </ B> <BR>
Each time the page is loaded, they automatically update the current value of counter. However, the page also contains a number of control components Counters can call the other two methods, that is, delete a counter (or set it to 0) and the counter set to a specified value, as shown in Figure 6-6:

Figure 6-6 Counters demonstrate the use of components
These controls in a <FORM>, click any button, a small gap, this form will be submitted to the same page, method and all the pages in this chapter used almost identical. The following procedure is for the Remove method of the HTML code to create controls.
<FORM ACTION = "<% = Request.ServerVariables (" SCRIPT_NAME ")%>" METHOD = "POST">

Counter.Remove ( "
<SELECT NAME="lstRemove" SIZE="1">
<OPTION VALUE="wrox"> Wrox Press </ OPTION>
<OPTION VALUE="stonebroom"> Stonebroom </ OPTION>
<OPTION VALUE="xtras"> Xtras </ OPTION>
<OPTION VALUE="compsrc"> ComponentSource </ OPTION>
<OPTION VALUE="fourcds"> Four CDs </ OPTION>
<OPTION VALUE="lunar"> Lunar </ OPTION>
</ SELECT> ") <P>
</ FORM>
When loading the page, the Request.Form collection to view by checking the button click, if the finding of a button, will run the corresponding part of the code. Click the Remove button in the circumstances, the corresponding code is:
If Len (Request.Form ( "cmdRemove")) Then
strCounterName = Request.Form ( "lstRemove") 'get the counter name
objCounters.Remove strCounterName
Response.Write "Removed counter '<B>" & strCounterName & "</ B>'. <P>"
End If
Methods for the Set, the situation is similar, but not only need to read the new value of text box, and in Set method calls before the text box to check whether the value of RMS.
If Len (Request.Form ( "cmdSet")) Then
strCounterName = Request.Form ( "lstSet") 'get the counter name
strNewValue = Request.Form ( "txtSet") 'get the new value
If IsNumeric (strNewValue) Then 'if it can be converted to a number
intNewValue = CInt (strNewValue) '... then convert it
objCounters.Set strCounterName, intNewValue
Response.Write "Set counter '<B>" & strCounterName & _
"</ B> 'to <B>" & strNewValue & "</ B>. <P>"
Response.Write "<B> '" & strNewValue & "</ B>' is not a valid number. <P>"
End If
End If
Use the button to call the page Counters object method, when re-loading the top of the page for some information and see the new counter value, as shown in Figure 6-7:

Figure 6-7 Object Counters call reload the page

6.2.5 Browser Capabilities Component
Create a variety of Web pages is one of the problems, not just the use of ASP technology to create dynamic web pages, and can use HTML elements, and other client-side technologies, like java Applets, ActiveX controls and HTML elements in the recent. Need to be aware of is that if the use of some visitors just do not support their browser, then carefully prepared for the web page, visitors may see the text of the messy combination of images, and even worse are the corresponding work script code.
It should be here not to discuss how to design supports a variety of different browser's home page (if you want to understand more the contents of this area can be prepared by Alex Homer inspection, Wrox publication "Professional ASP Techniques for Webmasters" book, ISBN is ISN 1 -- 861001-79-7). However, you sure you want to reference a page, ASP and IIS server to provide iron Browser Capabilities component browser can be used to detect the relevant characteristics of the support.
User requests a page from the server, the browser send the HTTP header contains the browser is using the details. In HTTP-speak (it was known as the user agent string), the definition of the browser name, version, operating system and its compatibility. Browser Capabilities component in their configuration file to find the string, and use many features of the same browser features. Therefore, running a web page at any time, Browser Capabilities component to provide support or not support the details of a particular characteristic.
In ASP 3.0 version, Browser Capabilities component adds a new features. Included in the ASP page METADATA directive instructions components removed from the browser a cookie, and to include any value added to the current component instance as a new property. This provides a way to collect from the browser more user-specific information, not just from the browscap.ini file is usually available the information specified in the browser. Understanding of the existing characteristics of how the browser detection work, go back on the new technology METADATA.
1. browscap.ini file
Browser Capabilities component to use a server-based version of the browscap.ini file, the text file must be in the same component file browscap.dll directory. browscap.ini file contains most of the previous and current information on the browser, and when the browser user agent string with the file do not match the specified string, it will use the default browscap.ini part of the document. Therefore, the browser add on new information or update the existing information, simply edit the browscap.ini file.
First of all, look at the browscap.ini file format, that document all entries are optional. Tam, including the default part is very important. If you are using a browser with the browscap.ini file do not match any one of, and do not specify the default browser settings, then all of the properties will be set up as "UNKNOWN".
The following is a browscap.ini file format:
; We can add comments anywhere, prefaced by a semicolon like this

; Entry for a specific browser
parent = browserDefinition
property1 = value1
property2 = value 2

[Default Browser capability Settings]
defaultProperty1 = defaultValue1
defaultProperty2 = defaultValue2
[HTTPUserAgentHeader] defines a specific line of the initial segment browser and Parent pointed out the line that contains more information browser another definition. The following definition of the line we want to get the Browser Capabilities component attributes, as well as for the corresponding value of the browser. If your browser is not listed in the respective paragraph, or even though it lists all the attributes are not listed, will be part of Default list and the corresponding value attribute.
For example, this file contains a [IE5.0] at the beginning of the paragraph, this paragraph have Internet Explorer 5.0 contains the corresponding value, There is no parent row, shown in (with the exception of those parts of the definition of Default) is that we only show the attributes of the definition.
[IE 5.0]
browser = IE
Version = 5.0
majorver = 5
minorver = 0
frames = TRUE
tables = TRUE
cookies = TRUE
backgroundsounds = TRUE
vbscript = TRUE
javascript = TRUE
javaapplets = TRUE
ActiveXcontrols = TRUE
Win16 = False
beta = False
AK = false
SK = false
AOL = false
Update = False
This description is not a browser and any match, because, HTTPUserAgentHeader line only [IE 5.0]. However, if the [IE 5.0] as a parent, you can add on for the IE5 browser definition:
[Mozilla/4.0 (compatible; MSIE 5 .*; Windows 95 *)]
parent = IE 5.0
version = 4.0
minorver = 0
platform = Win95
So we [IE 5.0] browser is designated as the parent, then the provision of explicit attributes will replace or add to the definition of the corresponding parent value, but here we have assumed that the value of any other attribute is not explicitly listed in its respective paragraph.
Very similar in order to identify the browser version, in line HTTPUserAgentHeader can use an asterisk wildcard, such as:
[Mozilla/4.0 (compatible; MSIE 5 .*; Windows 95 *)]
Will match the following statement:
[Mozilla/4.0 (compatible; MSIE 5.0; Windows95)]
[Mozilla/4.0 (compatible; MSIE 5.5; Windows 95 AOL)]
However, only send in your browser and the user agent string does not contain "*" HTTPUserAgentHeader not completely match the circumstances, only the use of a wildcard match. Only when this test fails, the string with a wildcard and will attempt to match the HTTPUserAgentHeader, and the use of paper to find the exact match of the first value.
Finally, the paragraph with the default browser:
[Default Browser Capability Settings]
browser = Default
Version = 0.0
majorver = # 0
minorver = # 0
frames = False
tables = True
cookies = False
backgroundsounds = False
vbscript = False
javascript = False
javaapplets = False
ActiveXcontrols = False
It is assumed that a worst-case scenario, the browser is almost nothing to support. Should be defined on this basis that we want to use the actual value. However, if the definition of a number of default values for the True, in a Unix terminal to use the browser text-only page, we might not reach the desired effect.
Browscap.ini file maintenance
When you close your browser to update the corresponding value Browscap.ini file browser to make it compatible with the characteristics of consistency, an increase of some old or because we are concerned is clearly the value of special is also very important. In order to provide convenience to users, usually from the Microsoft Web site to download to support the ASP's quite a comprehensive version of browscap.ini or upgrade version, and provided CrScape Inc version of browscap.ini often than Microsoft Web site to update the version number of .
Http://www.cyscape.com/browscap/ can find the latest version of browscap.ini, and a mailing list subscribers will automatically receive the latest version of the document. CrScape company also created a competition Microsoft Browser Capabilities Component component, known as browserHank (This chapter will introduce the back), new version of the browscap.ini file can also be obtained from the http://www.asptracker.com.
2. Browser Capabilities component to use
We have mastered the browscap.ini file contains information about how to provide information on a specific browser can be customized attribute, the following describes how to use the Browser Capabilities component. Relatively speaking, the use of some simple Browser Capabilities component, the following components to create an instance and its attributes.
<% 'In VBScript:
Set objBCap = Server.CreateObject ( "MSWC.BrowserType")
blnVBScriptOK = objBCap.vbscript 'save the result in a variable
If blnVBScriptOK Then
Response.Write "This browser supports VBScript"
Response.Write "This browser doesn't support VBScript"
End If
Procedure code above to check browser support VBScript and display a message, it is conceivable that this code is given based on the browser to respond to the different pages to guide users to a different page.
Of course, the use of the Browser Capabilities component attributes can be more complex than this work, a favorite of the technology it is based on the properties of the browser support for different loading site index page. If the site using a frame (frame) of the pages and do not use a frame page, when users visit the site the first time, to check the browser's ability to frame and re-position to the right index page.
3. Browser Capabilities using the cookie Characteristics
Browser Capabilities component of the new features is one of increase provides a way to get more information on the specific customer call page information.
browscap.ini file is only applicable to specific types of all browsers, so the report can only install the components of the common characteristics of the browser, for example, to know whether or not to support the browser cookie, but we can not know whether the user in a browser " Options "dialog box to close the cookie.
Similarly, the use of complex page design, it is best to understand the type of user to connect in order to choose the appropriate image file size to send to them, such as the users local area network (rather than the modem) connection, it allows us to provide a more rich environment. Users to know if the screen resolution, browsers, the language used, the type of operating system and processor parameters, the design for our help.
IE 5 through the use of such information to provide default behavior, which is a client-side page elements. IE 5 is the behavior of new, other browsers do not support, this is a page element to add special functions, through the STYLE attribute (or css style sheet) and the element link. In particular, IE 5 acts clientCaps provided can be used to provide the client and browser settings, as well as the current options.
Through the creation of an element and associated clientCaps behavior, through the elements of inquiry to the customer's information. The following pages provide examples from our files (browscap_cookie.htm) is to do so. It first defines the types used in <IE:clientcaps> contains all the elements of style clientCaps acts. This is the xml syntax, in the case of <HTML> Health XLMNS tags used for the definition of the current page namespace.
However, from the application page browscap_cookie.htm elements clientCaps be a series of values, and the establishment of a value that contains the cookie, Finally, the distribution of this cookie to the cookie file attributes, so that the directory server for this specific page request, to send it to the server.
IE \: clientcaps (behavior: url (# default # clientcaps))
</ HEAD>
<BODY ONLOAD="createCookie();">
<IE:clientcaps ID="objCCaps" />


function stopAllErrors () (
return true; / / prevent display of any errors

function createCookie () (
window.onerror = stopAllErrors;
var strCookie = new String ();
strCookie = 'width =' + objCCaps.width
+ '& Height =' + objCCaps.height
+ '& AvailWidth =' + objCCaps.availWidth
+ '& AvailHeight =' + objCCaps.availHeight
+ '& BufferDepth =' + objCCaps.bufferDepth
+ '& ColorDepth =' + objCCaps.colorDepth
+ '& JavaEnabled =' + objCCaps.javaEnabled
+ '& CookieEnabled =' + objCCaps.cookieEnabled
+ '& ConnectionType =' + objCCaps.connectionType
+ '& Platform =' + objCCaps.platform
+ '& CpuClass =' + objCCaps.cpuClass
+ '& SystemLanguage =' + objCCaps.systemLanguage
+ '& UserLanguage =' + objCCaps.userLanguage;
document.cookie = 'BrowsCap =' + strCookie;

</ BODY>
</ HTML>
In order to use the cookie, just the specific directives METADATA into the ASP page. As follows:
<! - METADATA TYPE = "Cookie" NAME = "BrowsCap"
SRC = "browserCapabilities / browscap_cookie.htm" ->
Now, run the ASP page, the page will automatically browscap_cookie.htm sent to the client, the client will run features of the act, and then return to cookie, then the Browser Capabilities component added to the contents of cookie component instance properties available list, query methods and query the attributes of the creation of browscap.ini file used by the same method.
width: <B> <% = objBCap.width%> </ B> <BR>
height: <B> <% = objBCap.height%> </ B> <BR>
Browser Capabilities page shows two types of series of sample values, one is from a decision by the browscap.ini file attributes in the data collected, and the other pages from the client cookie. Of course, not limited to only collect information from the client page in the value of clientCaps acts, the use of dynamic HTML browser technology can search for any property or navigator.appName such as the traditional object properties. Browser Capabilities component sample page shown in Figure 6-8:
Figure 6-8 Browser Capabilities component sample page

Asp Base Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.