I. Use querystring variable
Querystring is a simple and widely used method for transferring values, but it displays the passed values in the address bar of the browser, this method can be used to transmit one or more numeric values with low security requirements or simple structure.
Response. Redirect ("target. aspx? Param1 = Hello & param2 = Hi ")
Receiving page: String STR = request. querystring ["param1"];
String str1 = request. querystring ["param2];
2. Use the cookie object variable (the cookie is stored on the client)
Set COOKIE: httpcookie cookie_name = new httpcookie ("name ");
Cookie_name.value = label1.text;
Reponse. appendcookie (cookie_name );
Get COOKIE:
String name = request. Cookie ["name"]. value. tostring ();
3. Use session variables (sessions are stored on the server)
Set session: session ["name"] = "hello ";
Get session: string name = session ["name"]. tostring ();
4. Use the application object variable
The scope of the Application object is global, that is, it is valid for all users. This method is not often used. Because the application is shared within an application domain, all users can change and set its value. Therefore, only the counters and other places that require global variables are used.
Set Application: application ["name"] = "hello ";
Obtain Application: string name = application ["name"]. tostring ();
5. postbackurl () method
Default. aspx page:
Code
1 <asp: button id = "button1" runat = "server" text = "posttoanotherpage" postbackurl = "~ /Default2.aspx "/>
2
Default2.aspx page:
Code
1 If (previouspage! = NULL)
2 {
3 textbox textbox1 = (textbox) previouspage. findcontrol ("textbox1 ");
4 response. Write (textbox1.text );
5}
6. Use the server. Transfer Method
This can be said to be the method used for face object development. It uses server. the transfer method directs the process from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is completely object-like and concise and effective. The following code shows the method used when many parameters are required. If there are few parameters, it is unnecessary to use this method.
If you want all query pages to inherit an interface and define a method in this interface, the only function of this method is to obtain the parameters required for building the result on the result page, you can share multiple pages with one result page!
1. Define a class and place all query parameters with the class:
Code
/** // <Summary>
/// Summary of queryparams
/// </Summary>
Public class queryparams
{
Private string firstname;
Private string lastname;
Private int age;
Public String firstname
{
Get {return this. firstname ;}
Set {This. firstname = value ;}
}
Public String lastname
{
Get {return this. lastname ;}
Set {This. lastname = value ;}
}
Public String age
{
Get {return this. Age ;}
Set {This. Age = value ;}
}
}
2. Interface Definition:
Code
/** // <Summary>
/// Define the query interface.
/// </Summary>
Public interface iqueryparams
{
/** // <Summary>
/// Parameters
/// </Summary>
Queryparams parameters {Get ;}
}
3. the query page inherits the iqueryparams interface (querypage. aspx ):
Querypage. aspx
Code
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "txtfirstname" runat = "server"> </ASP: textbox>
<Asp: textbox id = "txtlastname" runat = "server"> </ASP: textbox>
<Asp: textbox id = "txtage" runat = "server"> </ASP: textbox>
<Asp: button id = "btnenter" runat = "server" text = "button" onclick = "btnenter_click"/> </div>
</Form>
Querypage. aspx. CS
Code
Public partial class querypage: system. Web. UI. Page, iqueryparams
{
Private queryparams;
Public queryparams Parameters
{
Get
{
Return queryparams;
}
}
Public void btnenter_click (Object sender, system. eventargs E)
{
// Assign a value
Queryparams = new queryparams ();
Queryparams. firstnname = this.txt firstname. text;
Queryparams. lastname = this.txt lastname. text;
Queryparams. Age = this.txt age. text;
Server. Transfer ("resultpage. aspx ");
}
Protected void page_load (Object sender, eventargs E)
{}
}
4. Receiving page (resultpage. aspx ):
Resultpage. aspx. CS
Public partial class resultpage: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Queryparams = new queryparams ();
Iqueryparams QueryInterface;
// Page for implementing this interface
If (context. handler is iqueryparams)
{
QueryInterface = (iqueryparams) Context. Handler;
Queryparams = QueryInterface. parameters;
}
Response. Write ("firstname :");
Response. Write (queryparams. firstname );
Response. Write ("<br/> lastname :");
Response. Write (queryparams. lastname );
Response. Write ("<br/> Age :");
Response. Write (queryparams. Age );
}
}
1. Keep the position of the scroll bar after callback
In ASP. net1.1, it is very painful to keep the position of the scroll bar after callback, especially when there is a grid in the page and you want to edit a Specific Row. The page is reloaded and must be rolled down at the top of the page to keep the page unchanged. In asp2.0, you only need to simply add the maintainscrollpostiononpostback attribute to the page attribute:
<% @ Page Language = "C #" maintainscrollpositiononpostback = "true" autoeventwireup = "true" codefile = "" inherits = "" %>
2. After the page is loaded, set the default focus to the control.
This is also a very simple example, which can be completed without the help of JavaScript. If there are two textbox in the page, why do you want the user to click textbox to start inputting data? Can the cursor be placed in the textbox to input data? You can easily use the defaultfocus attribute of the htmlform control:
<Form ID = "frm" defaultfocus = "txtusername" runat = "server">
</Form>
3. Click the default button when you click Enter.
In asp1.1, it is very painful to enable the user to click the "enter" key to associate the Click Event of the server segment of a button with JavaScript. Fortunately, now you can use the defaultbutton attribute of the htmlform control to set it. This attribute can also be set on the panel control. When a user moves to a different Panel on the page, click Enter to trigger the click events of different button controls.
<Form ID = "frm" defaultbutton = "btnsubmit" runat = "server">
</Form>
4. Simple search for fixed controls.
It is very painful to search for controls on the page by hierarchy. But if you know how the controls are fixed on the page, you can use the abbreviation "$" to search for controls without writing recursive code. Please refer to the following code and pay attention to the usage of "$:
<Form ID = "form1" runat = "server" defaultfocus = "formvw $ txtname">
<Div>
<Asp: formview id = "formvw" runat = "server">
<Itemtemplate>
Name:
<Asp: textbox id = "txtname" runat = "server"
TEXT = '<% # eval ("firstname") + "" + eval ("lastname") %>'/>
</Itemtemplate>
</ASP: formview>
</Div>
</Form>
This tip can also be used when the findcontrol () function is used on the server side:
Textbox TB = This. findcontrol ("form1 $ formvw $ txtname") as textbox;
If (TB! = NULL)
{
// Access Textbox Control
}
5. Controls for cross-Page Submission with strong type access
This is more useful than others. ASP. NET 2.0 introduces the concept of cross-sending so that a page can send information back to another different page. You can set the postbackurl attribute of the button control to the page for receiving the send-back data. Generally, the resend data can do something like the previous page. However, if you want to obtain the attributes of the control set on the previous page, you need a cast (). If you add a public attribute to the code-behide page that causes the sending back, you can directly add previouspagetype in a strongly typed manner to point to the page that causes the sending back to access that attribute.
If there is a page default. aspx, which has a public attribute to return a textbox on this page and the data sending target page (searchresults. aspx) can be added to the top of the page in a strongly typed mode (the findcontrol () method is not required:
<% @ Previouspagetype virtualpath = "default. aspx" %>
In this way, the code in searchresults. aspx can access the textbox of default. aspx in a strongly typed manner. The following example assumes that the property defined by default. aspx is searchtextbox:
Textbox TB = previouspage. searchtextbox; previouspagetype also has a typename attribute. You can define a basic type and inherit one or more pages from this type, so that this technology can be used on multiple pages.
Previouspage. iscrosspagepostback can be used to determine whether the page is submitted.
Supplement cross-page submission:
In ASP. NET 1.x, all pages are submitted to themselves, and it is not convenient to specify the target page to be submitted. For example, a button in firstpage. aspx can only be submitted to firstpage. aspx, but not secondpage. aspx. Most of the time, the way ASP. NET 1.x works imposes many restrictions on our development methods. Friends who are familiar with ASP, JSP, and PHP are probably not used to it, because the submission method that is frequently used in the past suddenly cannot be used, although there are also ways to solve this problem (readers who want to know more about it can watch webcast on the Microsoft website), the process is too cumbersome and inconvenient. We are glad that ASP. NET 2.0 has a simple cross-Page Submission method. You can add the postbackurl attribute on the button on the first page to the page that accepts the submission, and add the previouspagetype command on this page, if the target page is opened in a new window, you can add the target = '_ blank' attribute to the <form> mark on the Source Page.
6. Controls for accessing the master page with a strong type
The previouspagetype command is not the only method that allows strong-type access to controls. If you define a public attribute on the master page and want to access it in a strongly typed manner, you can add the mastertype command at the top of the page (note: the mastertype command can define a typename like the previouspagetype command)
<% @ Mastertype virtualpath = "masterpage. Master" %>
You can write the following code on the Content Page to access the attributes of the Target Master page:
This. master. headertext = "label updated using mastertype directive with virtualpath attribute .";
7. Validation Group
A page can contain multiple controls and buttons. When one of the buttons is clicked, you want a specific validator to be fired, instead of all validator on the page. There is no better way except hack code in ASP. NET 1.1. In ASP. NET 2.0, the validator group attribute is added to all validator controls and buttons (such as buttons and linkbuttons) to easily solve this problem. If there is a textbox in the page and there is a requiredfieldvalidator and button control next to it, you can set the validationgroup attributes of requiredfieldvalidator and button to the same value so that only the validator of requiredfieldvalidator is triggered when you click button. Any other validator that is not defined in the validationgroup will be ignored. See the following example:
<Form ID = "form1" runat = "server">
Search Text: <asp: textbox id = "txtsearch" runat = "server"/>
<Asp: requiredfieldvalidator id = "valsearch" runat = "server"
Controltovalidate = "txtsearch" validationgroup = "searchgroup"/>
<Asp: button id = "btnsearch" runat = "server" text = "Search"
Validationgroup = "searchgroup"/>.
Other controls with validators and buttons defined here
</Form>