Forum favorites: ASP/ASP. NET

Source: Internet
Author: User

**************************************** *****************************
Title: Web printing, simple implementation
Source: http://community.csdn.net/Expert/topic/2959/2959189.xml? Temp =. 9849207.
**************************************** *****************************
<! -- Save language independence as. HTML -->
<Html>
<Head>
<Meta name = vs_targetSchema content = "http://schemas.microsoft.com/intellisense/ie5">
<Title> View </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<! -- Media = print this attribute can be valid during printing -->
<Style media = print>
. Noprint {display: none ;}
. PageNext {page-break-after: always ;}
</Style>

<Style>
. Tdp
{
Border-bottom: 1 solid #000000;
Border-left: 1 solid #000000;
Border-right: 0 solid # ffffff;
Border-top: 0 solid # ffffff;
}
. Tabp
{
Border-color: #000000 #000000 #000000 #000000;
Border-style: solid;
Border-top-width: 2px;
Border-right-width: 2px;
Border-bottom-width: 1px;
Border-left-width: 1px;
}
. NOPRINT {
Font-family: "";
Font-size: 9pt;
}

</Style>

</Head>

<Body>
<Center class = "Noprint">
<P>
<OBJECT id = WebBrowser classid = CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2 height = 0 width = 0>
</OBJECT>
<Input type = button value = print onclick = document. all. WebBrowser. ExecWB (6, 1)>
<Input type = button value = print onclick = document. all. WebBrowser. ExecWB (6, 6)>
<Input type = button value = page setting onclick = document. all. WebBrowser. ExecWB (8, 1)>
</P>
<P> <input type = button value = print preview onclick = document. all. WebBrowser. ExecWB (7,1)>
<Br/>
</P>
<Hr align = "center" width = "90%" size = "1" noshade>
</Center>

<Table width = "90%" border = "0" align = "center" cellpadding = "2" cellspacing = "0" class = "tabp">
<Tr>
<Td colspan = "3" class = "tdp"> page 1 </td>
</Tr>
<Tr>
<Td width = "29%" class = "tdp"> & nbsp; </td>
<Td width = "28%" class = "tdp"> & nbsp; </td>
<Td width = "43%" class = "tdp"> & nbsp; </td>
</Tr>
<Tr>
<Td colspan = "3" class = "tdp"> & nbsp; </td>
</Tr>
<Tr>
<Td colspan = "3" class = "tdp"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<Td width = "50%" class = "tdp"> <p> reports like this </p>
<P> general requirements are sufficient. </P> </td>
<Td> & nbsp; </td>
</Tr>
</Table> </td>
</Tr>
</Table>
<Hr align = "center" width = "90%" size = "1" noshade class = "NOPRINT">
<! -- Pagination -->
<Div class = "PageNext"> </div>
<Table width = "90%" border = "0" align = "center" cellpadding = "2" cellspacing = "0" class = "tabp">
<Tr>
<Td class = "tdp"> page 2nd </td>
</Tr>
<Tr>
<Td class = "tdp"> the page is displayed </td>
</Tr>
<Tr>
<Td class = "tdp"> & nbsp; </td>
</Tr>
<Tr>
<Td class = "tdp"> & nbsp; </td>
</Tr>
<Tr>
<Td class = "tdp"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<Td width = "50%" class = "tdp"> <p> reports like this </p>
<P> general requirements are sufficient. </P> </td>
<Td> & nbsp; </td>
</Tr>
</Table> </td>
</Tr>
</Table>
</Body>
</Html>

**************************************** *****************************
Title: session, viewstate, and cookies
Source: http://community.csdn.net/Expert/topic/3043/3043099.xml? Temp =. 2811701.
**************************************** *****************************
ViewState is a new way of State Preservation proposed in. Net (in fact, it is also a new wine in old bottled wine). We know that there are several ways to save the state of traditional Web programs:
1. Application: This is the global storage area of the Web Application in its life cycle. The data stored in the Application is globally valid. net, there is an application pool, which stores several (or dozens) application instances, each request will take an instance from the pool to process the request, before the request is completed, this instance will not accept other requests. This leads to a problem where multiple applications, namely multiple threads, may exist at the same time, these threads all have the possibility of accessing the Application. Therefore, thread synchronization needs to be considered when processing objects in the Application. In fact, the Application object implements a thread lock, the lock and unlock operations are automatically called when you call its own Add, Remove, and other methods. However, for performance considerations, for the process of obtaining and operating the objects directly through the indexer or other methods, the Application does not automatically process thread synchronization, and the following similar code must be used for processing:
Application. Lock ();
(Int) Application ["Count"]) ++;
Application. Unlock ();
It is worth noting that after the Lock is called, if Unlock is not displayed, the Application object will be automatically unlocked at the end of the request, which prevents deadlock, however, to ensure code robustness, the Unlock method should be called immediately after the Lock is called and the modification is completed.
The Application object is essentially a Hash table, which stores objects according to the key value. Because the objects are global and stored on the server, and multiple threads are simultaneously accessed, the Application stores data that is frequently accessed, rarely modified, and used by at least a majority of global functions, such as counters or database connection strings.

2. Session in Asp. net internal, there is a StateApplication to manage the Session, in fact, is a helper process, processing Session expiration, create special requests, when receiving each request, the auxiliary process will call the status server (you can use the Web. config sets different status servers) to obtain the Session. If there is no Session for which the SessionId should be set, a new Session will be created and bound to the context (HttpContext). Unlike Asp, there are multiple Session Status servers, currently in Asp. net has three internal implementations:
1) InProcStateClientManager: this is a traditional Session storage method, but there are still some nuances.
2) SqlStateClientManager: saves the Session to the database.
3) OutOfProcStateClientManager: saves the Session outside the process.
The Session mechanism of Asp. Net has a feature that the auxiliary process for processing the Session is separated from the State server for storing the Session. According to MSDN, the following benefits are provided:
"Because the memory used for session status is not in the ASP. NET auxiliary process, you can recover from application faults ."
"Because all statuses are not stored together with the secondary process, you can partition the application across multiple processes cleanly. This partition can significantly improve the availability and scalability of applications on computers with multiple processes ."
"Because all statuses are not stored together with the secondary processes, You can partition applications across multiple secondary processes running on multiple computers ."
Asp. in my opinion, the Session mechanism of Net is flexible, and the internal implementation is also clever. But in fact, because there is not much test done, will the application be as beautiful as it said, do not dare to pack tickets. I will write an article separately to discuss the Session mechanism in Asp. Net.

3. There is nothing to say about Cookie. In fact, there is no difference between Asp. Net and Asp cookies. This technology may be mixed and relies on client implementation, and MS has no improvement.

4. ViewState: This is what we will focus on today. In fact, ViewState is not mysterious. It is a Hidden field, but it is the basis for saving the status of server controls; unfamiliar friends can use IE to view the Html source code and find a Hidden field named "_ VIEWSTATE". There are a lot of messy characters, which are the ViewState of the page.

People who have done Web programs may have this kind of painful experience. Sometimes, in order to process complex functions on the page, many Hidden operations are often added, then, the server uses a lot of judgments to analyze the current status, which is annoying. It is even more difficult to write the code. In fact, ViewState helps our system implement the function of saving the control status, the server control can be saved among multiple requests.

Well, the introduction is here. Today we are not discussing the use of ViewState, but exploring the essence of this thing from the inside.
First, create a test page:
<% @ Page language = "c #" Codebehind = "ViewStateTest. aspx. cs" AutoEventWireup = "false" Inherits = "CsdnTest. ViewStateTest" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<Title> ViewStateTest </title>
<Meta name = "GENERATOR" Content = "Microsoft Visual Studio 7.0">
<Meta name = "CODE_LANGUAGE" Content = "C #">
<Meta name = "vs_defaultClientScript" content = "JavaScript">
<Meta name = "vs_targetSchema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body>
<Form id = "ViewStateTest" method = "post" runat = "server">
<Asp: Button ID = "btnPostBack" Runat = "server" Text = "Post Back" Width = "85px"> </asp: Button>
<Br/>
<Asp: CheckBox ID = "chkTest" Runat = "server" Text = "This is a check box"> </asp: CheckBox>
</Form>
</Body>
</Html>

This is a simple page designed with Vs. Net. It contains a server-side button and a CheckBox, And then we respond to the button event on the server side:

Private void btnPostBack_Click (object sender, System. EventArgs e)
{
[1] Response. Write ("ViewState:" + Request. Params ["_ VIEWSTATE"] + "<br/> ");

[2] string decodeValue = Encoding. UTF8.GetString (Convert. FromBase64String (Request. Params ["_ VIEWSTATE"]);

[3] Response. Write ("ViewState decode:" + decodeValue + "<br/> ");

[4] object viewstate = (new LosFormatter (). Deserialize (Request. Params ["_ VIEWSTATE"]);
 
[5] Response. Write ("ViewState Object:" + viewstate. GetType (). Name );
}

For convenience, I added a row number. In the first line, we typed out the value of ViewState. What is the second line? In fact, a string stored in ViewState on the client is the internal ViewState serialized in some way and then encoded in Base64. Therefore, we can re-encode the Base64 encoded string; as for the fourth line, I will not talk about it first. Let's look at the execution result first:
After running, there is nothing on the page, except the button and CheckBox (nonsense :), we click the button and the result is as follows:

[A] ViewState: dDwxMjU2MDI5MTA3OztsPGNoa1Rlc3Q7Pj6Gg0Qzm + 7gacYWcy0hnRCT9toOdA =
[B] ViewState decode: t <1256029107; l> D3i s -! T
[C] ViewState Object: Triplet

Then let's analyze this result. The value displayed in A is the value uploaded from ViewState to the client. The value displayed in B is the value after Base64 anti-encoding. It seems that nothing can be seen from this, in C, A: Triplet? What is this? Let's go back to the above Code:
Object viewstate = (new LosFormatter (). Deserialize (Request. Params ["_ VIEWSTATE"]);

Note that we use a LosFormatter class. In fact, this class is Asp. net provides a serialization class for ViewState. It has two methods: Serialize, which is to Serialize an object, Deserialize, and deserialization, here we use the deserialization method to directly deserialize ViewState into an object, and then type this object. This object is the Triplet type, in fact Asp. the ViewState stored on the page in. Net is of this type. Let's analyze the LosFormater and then explain it in detail.
Let's look back at the result B: t <1256029107; l> D3i s -!
T. In fact, by viewing the LosFormatter decompiled code, we can see that the serialization method is very simple, that is, to judge the type of the object to be serialized. If it is not a directly serialized type, record its type and recursively serialize its attributes. We can see that "t" in B Indicates the Triplet type, which has three attributes, these three attributes are included between "<" and ">", separated by ";", and the final D3i s -!
T according to my analysis, it should be a Hash value that prevents ViewState changes. This is not very definite, because the decompilation code is really ugly. I just did not read it carefully after learning it.

We have just analyzed that the ViewState deserialization in the Page is the Triplet type. In fact, this class is found in MSDN. It is an object containing three objects, which is simple, it is a big box that can store three boxes (it seems to be confused, haha). It has three attributes: First, Second, Thrid :), representing three objects respectively.
Corresponding to Page, First is Page. the return value of GetTypeHashCode (). This method is System. web. UI. A protected virtual method defined by Page, returns an integer, implemented by the class generated by the Aspx file, because this class has Asp. net is responsible for generating and compiling the source code at runtime. It calculates a large constant as the return value, which is unique in all pages of the Web application. (Put forward an external question, Asp. net automatically generated source code can be to the System Disk:/WINDOWS/Microsoft. NET/Framework/v1.0.3705/Temporary ASP. NET Files). The unique Hash value is used to generate a tag in ViewState, so that this ViewState applies only to the corresponding page.
Second recursively saves the object returned by ViewState of the page Control tree through the Control. SaveViewStateRecursive method, that is, the real ViewState data.
Third stores the list of control names on the current page that require PostBack.

After analyzing the composition of the ViewState of the page, let's look at the implementation of the ViewState of Control. ViewState is System. web. UI. A Property Implemented by the Control class. The type of this property is System. web. UI. stateBag: This class includes the implementation of ViewState data structure. In fact, it is an internal Hash table that stores and retrieves data through Key values.
How does the Server Control Save the status?
We know that all server controls are derived from System. Web. UI. Control, so they all have the ViewState attribute. Within Control, two Protected virtual methods are defined:
Protected virtual object SaveViewState ()
And
Protected virtual void LoadViewState (object savedState)

These two methods are used to derive the child control to save and read its own ViewState. For example, we have a self-written control that saves a string to ViewState, then our method is roughly like this:
Protected virtual object SaveViewState ()
{
Object [] states = new object [2];
States [0] = base. SaveViewState (); // remember to save the ViewState of the parent Control
States [1] = "Hello, I'm tietong! "; // Save your own

Return states; // return the repackaged object
}
When obtaining:
Protected override void LoadViewState (object savedState) // The savedState here is the object array returned when we Save
{
Object [] states = (object []) savedState;
Base. LoadViewState (states [0]); // parse the data of the parent class for himself.
String myData = (string) states [1]; // obtain our own data
}
We can save it in our own way. We don't have to use arrays like above. In fact, we can use any object that supports serialization. The parent class doesn't care about how to save subclass, we only need to use the same method when saving and Load, and pass the correct data to the parent class method.

Another problem is that the ViewState of the Control we use is a Key-Value Pair such as Key-Value. How does it save it?
In fact, it is very simple. There is a class named Pair under System. UI. Web. It is similar to Triplet, but it only contains two objects. When StateBag is saved, First stores the array of all Key values, and Second stores the array of all values.

Now, we have learned how ViewState is serialized and saved to the client, and how controls save their own ViewState. How are these two combined?

That is, how does the ViewState of the Control tree of the entire page be saved and read?

There are two internal methods in Control:
Internal object SaveViewStateRecursive ();
Internal void LoadRecursive ();
These two methods are composed of System. web. UI. the SavePageViewState method is called by Page after the Render ends. The SavePageViewState method calls the SaveViewStateRecursive () method of Control. This method uses recursion to call each Control. the SaveViewStateRecursive method of Controls is used to save the ViewState of all Controls in the control tree. At this point, a wise friend may ask, since SaveViewStateRecursive is a recursive call to save the method, what is the use of the SaveViewState () method we wrote above?
We know that Control. there may be many Controls, and our SaveViewState () only saves the data of the current control, but does not record the structure of the Control tree, so if we recursive SaveViewState () if the method is used to save data, the structure of the Control tree will be lost, so the structure of the Load cannot be restored. In fact, the code in the SaveViewStateRecursive method is as follows:
[1] obtain the ViewState of the control (call the SaveViewState method)
[2] loop child Control
{
Define two dynamic arrays, one for storing the control index, and one for storing the values returned by recursively calling the SaveViewStateRecursive method of the child control.
}
[3] define a Triplet (haha, this thing appears again)
[4] First save the ViewState of this control
[5] Second: Save the sub-Control Index
[6] Third: Save the return value of the SaveViewStateRecursive method of the recursive subcontrol
[7] returns Triplet
In this way, the ViewState of the entire control tree and the structure of the Control tree are saved.

The Load method is similar to the Save method, but the Load method retrieves the index of the sub-control from the savedState to recursively recursive the LoadRecursive () method of the sub-control, this ensures that the stored data is correctly transmitted to the Child control.

Here, we have a general understanding of the implementation of ViewState, and finally come to some conclusions:
1. ViewState is stored on the client, which reduces the burden on the server and is a better way to save data.
2. Because of the limitations of ViewState, only serializable objects can be saved, and it is best not to put too many things, saving the province, so as not to slow down the transmission speed and increase the load on Server Resolution.
3. We can obtain the value in ViewState in a simple way. We have discussed some of the above, although we have not written the parsed code, however, the LosFormatter can be used to obtain the object after ViewState deserialization, which is easy to parse. Therefore, ViewState is still relatively poor in terms of security. We recommend that you do not
Stores confidential and sensitive information. Although ViewState can be encrypted, because ViewState is stored on the client, security risks are inherent.
4. From the technical point of view, ViewState has no new ideas, but the design of server controls is clever.

Finally, from my personal point of view, I think the appearance of ViewState has greatly reduced the burden on programmers, but what we need to see is the nature of ViewState and the rational application of it.

**************************************** *****************************
Title: it is not a thumbnail, but can be scaled down in proportion. It is useful in the product list.
Source: http://community.csdn.net/Expert/topic/3111/3111151.xml? Temp =. 9140741.
**************************************** *****************************
<%
Class possible
Dim aso
Private Sub Class_Initialize
Set aso = CreateObject ("Adodb. Stream ")
Aso. Mode = 3
Aso. Type = 1
Aso. Open
End Sub
Private Sub Class_Terminate
Set aso = nothing
End Sub

Private Function Bin2Str (Bin)
Dim I, Str
For I = 1 to LenB (Bin)
Clow = MidB (Bin, I, 1)
If ASCB (clow) <128 then
Str = Str & Chr (ASCB (clow ))
Else
I = I + 1
If I <= LenB (Bin) then Str = Str & Chr (ASCW (MidB (Bin, I, 1) & clow ))
End if
Next
Bin2Str = Str
End Function
 
Private Function Num2Str (num, base, lens)
Dim ret
Ret = ""
While (num> = base)
Ret = (num mod base) & ret
Num = (num-num mod base)/base
Wend
Num2Str = right (string (lens, "0") & num & ret, lens)
End Function
 
Private Function Str2Num (str, base)
Dim ret
Ret = 0
For I = 1 to len (str)
Ret = ret * base + cint (mid (str, I, 1 ))
Next
Str2Num = ret
End Function
 
Private Function BinVal (bin)
Dim ret
Ret = 0
For I = lenb (bin) to 1 step-1
Ret = ret * 256 + ascb (midb (bin, I, 1 ))
Next
BinVal = ret
End Function
 
Private Function BinVal2 (bin)
Dim ret
Ret = 0
For I = 1 to lenb (bin)
Ret = ret * 256 + ascb (midb (bin, I, 1 ))
Next
BinVal2 = ret
End Function
 
Private Function getImageSize (filespec)
Dim ret (3)
Aso. LoadFromFile (filespec)
BFlag = aso. read (3)
Select case hex (binVal (bFlag ))
Case "4E5089 ":
Aso. read (15)
Ret (0) = "PNG"
Ret (1) = BinVal2 (aso. read (2 ))
Aso. read (2)
Ret (2) = BinVal2 (aso. read (2 ))
Case "464947 ":
Aso. read (3)
Ret (0) = "GIF"
Ret (1) = BinVal (aso. read (2 ))
Ret (2) = BinVal (aso. read (2 ))
Case "535746 ":
Aso. read (5)
BinData = aso. Read (1)
SConv = Num2Str (ascb (binData), 2, 8)
NBits = Str2Num (left (sConv, 5), 2)
SConv = mid (sConv, 6)
While (len (sConv) <nBits * 4)
BinData = aso. Read (1)
SConv = sConv & Num2Str (ascb (binData), 2, 8)
Wend
Ret (0) = "SWF"
Ret (1) = int (abs (Str2Num (mid (sConv, 1 * nBits + 1, nBits), 2)-Str2Num (mid (sConv, 0 * nBits + 1, nBits), 2)/20)
Ret (2) = int (abs (Str2Num (mid (sConv, 3 * nBits + 1, nBits), 2)-Str2Num (mid (sConv, 2 * nBits + 1, nBits), 2)/20)
Case "FFD8FF ":
Do
Do: p1 = binVal (aso. Read (1): loop while p1 = 255 and not aso. EOS
If p1> 191 and p1 <196 then exit do else aso. read (binval2 (aso. Read (2)-2)
Do: p1 = binVal (aso. Read (1): loop while p1 <255 and not aso. EOS
Loop while true
Aso. Read (3)
Ret (0) = "JPG"
Ret (2) = binval2 (aso. Read (2 ))
Ret (1) = binval2 (aso. Read (2 ))
Case else:
If left (Bin2Str (bFlag), 2) = "BM" then
Aso. Read (15)
Ret (0) = "BMP"
RET (1) = binval (ASO. Read (4 ))
RET (2) = binval (ASO. Read (4 ))
Else
RET (0) = ""
End if
End select
RET (3) = "width =" & RET (1) & "" Height = "& RET (2 )&""""
Getimagesize = RET
End Function
 
Function readx (pic_path)
Set fso1 = server. Createobject ("scripting. FileSystemObject ")
Set F1 = fso1.getfile (pic_path)
EXT = fso1.getextensionname (pic_path)
Select case ext
Case "GIF", "BMP", "jpg", "PNG ":
Arr = getimagesize (f1.path)
Response. Write Arr (1)
Case "swf"
Arr = pp. getimagesize (f1.path)
Response. Write arr (1)
End select
Set f1 = nothing
Set fso1 = nothing
End Function

Function readY (pic_path)
Set fso1 = server. CreateObject ("Scripting. FileSystemObject ")
Set f1 = fso1.GetFile (pic_path)
Ext = fso1.GetExtensionName (pic_path)
Select case ext
Case "gif", "bmp", "jpg", "png ":
Arr = getImageSize (f1.path)
Response. Write arr (2)
Case "swf"
Arr = pp. getimagesize (f1.path)
Response. Write arr (2)
End select
Set f1 = nothing
Set fso1 = nothing
End Function
End Class
%>

Example:

<! -- # Include file = "picXY. asp" -->
<%
Set pp = new possible
Pp. readX ("E:/work/bg.jpg ")
Pp. readY ("E:/work/bg.jpg ")
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.