Call the background code on the front-end page

Source: Internet
Author: User
Tags xsl file

1. Several similar markup symbols in ASP. NET: <% =... %> <% #... %> <% @ %> explanation and usage

A: <% #... %>: it is executed when the databind () method of the bound control is executed and used for data binding.

For example: <% # container. dataitem ("tit") %>

<% = %>: Called during program execution. The background variable value can be displayed.

For example:

*. In aspx: <% = aaa %>

*. CS: protected string AAA = "name ";

<%>: The Inline code block can be embedded with background code in the page file *. aspx or *. ascx.

For example:

<%

For (INT I = 0; I <100; I ++)

{

Reaponse. Write (I. tostring ());

}

%>

<% @ %> Is the front-end code import namespace on the *. ASPX page,

For example:

<% @ Importnamespace = "system. Data" %>

2. What types of data does the control receive?

A: controls that receive bind, including dropdownlist, datalist, DataGrid, and ListBox, which are mainly associated with arraylist and hashtable ), dataview (data view) and datareader can be seated in the future without the error that the able is bundled :)

3. The data obtained by databind will be converted to another type by default as string?

Databinder. (container. dataitem, "Conversion Type", "format ")

The last "format" is optional. Generally, you do not need to worry about it. Container. dataitem is a bundled data item. "Conversion Type" refers to integer, String, Boolean.

4. Main namespace:

<% @ Import namespace = "system. Data" %> used to process data

<% @ Import namespace = "system. Data. Ado" %> used when using ado.net;

<% @ Import namespace = "system. Data. SQL" %> dedicated to SQL Server databases

<% @ Import namespace = "system. Data. xml" %> you do not need to view

<% @ Import namespace = "system. Io" %> used for File Processing

<% @ Import namespace = "system. Web. util" %>

<% @ Import namespace = "system. Text" %> used for Text Encoding

5. common attributes and methods of connections (sqlconection or adoconnection:

| Connectionstring: gets or sets the database connection statement.

| Connectiontimeout: gets or sets the maximum time for connecting to the database, which is also the timeout time.

| Database gets or sets the name of the database to be opened on the database server

| Datasource: Get or set DSN. No stranger to everyone :)

| Password

| Userid: Get or set the login name

| State: gets the current join state.

| Open () Open the connection

| Close () Close the connection

| Clone (): clone a join. (Hehe, the sheep can connect or I can)

Example:

Sqlconnection myconnection = new sqlconnection ();

Myconnection. datasource = "mysqlserver ";

Myconnection. Password = "";

Myconnection. userid = "sa ";

Myconnection. connectiontimeout = 30;

Myconnection. open ();

Myconnection. Database = "northwind ";

Myconnection. isolationlevel = isolationlevel. readcommitted

6. common methods and attributes of command

| Activeconnection

| Name of the SQL statement or stored procedure (storedprocedure) executed by commandtext

| Maximum commandtimeout execution time

| Commandtypecommand operation types (storedprocedure, text, tabledirect). The default value is text.

| Parameters

| Execute () Execute SQL statements or stored procedures

| Executenonquery () is the same as above. The difference is that the record set is not returned.

| Clone () clone command

Example:

String myselectquery = "select * from categories order bycategoryid ";

Stringmyconnectstring = "userid = sa; Password =; database = northwind; server = mysqlserver ";

Sqlcommand mycommand = new sqlcommand (myselectquery );

Mycommand. activeconnection = newsqlconnection (myconnectstring );

Mycommand. commandtimeout = 15;

Mycommand. commandtype = commandtype. Text; </font>

7. You can open or close a database in either of the following ways:

1. myconnection. open (); // open the connection

Myconnection. Close ();

2. mycommand. activeconnection. open ();

Mycommand. activeconnection. Close ()

8. Use dataset to add, modify, and delete data in the database.

A. add data

Datarow DR = mydataset. Tables ["userlist"]. newrow ();

Dr ["username"] = "Zhou Xun ";

Dr ["remark"] = "100 ";

Dr ["comment"] = "Beautiful mm ";

Mydataset. Tables. Rows. Add (DR );

B. modify data

Mydataset. Tables ["userlist"]. Rows [0] ["username"] = "Flying knife ";

C. delete data

Mydataset. Tables ["userlist"], rows [0]. Delete ();

D. Restore data

If (mydataset. haserrors)

{

Mydataset. rejectchanges ();

}

E. Check whether dataset has been modified.

If (mydataset. haschanges)

{

// Save the code

} Else {

// Because there is no change, you do not need to save it to save time

}

F. update the database

Mycomm. Update (mydataset); // update all tables in the database

Mycomm. Update (mydataset, "userlist"); // update a table

9. The DataGrid provides the paging function.

Allowpaging = "true" // indicates that pagination is allowed. With it, we can paging.

Pagesize = "5" // specifies the number of records displayed on each page. If this parameter is not set, 10 records are displayed by default.

Pagerstyle-horizontalalign = "right" // specifies the position of the split display. The default value is left.

Pagerstyle-nextpagetext = "next page" // change <> to the string of the previous page and next page.

Pagerstyle-prevpagetext = "Previous Page"

Pagerstyle-mode = "numericpages" // change <> to 123 Numeric Display

10. display the total number of pages and the current report is the page number

Current page: <% = datagrid1.currentpageindex + 1%>

Total number of pages: <% = datagrid1.pagecount %>

11. Personalized Paging

Programmer base camp "close access to ASP. NET (14)" with complete code

12. You need to reset the page to a valid status.

Ivalidator val;

Foreach (Val in validators)

{

Val. isvalid = true;

}

13. Re-execute the entire verification sequence

Ivalidator val;

Foreach (Val in validators)

{

Val. Validate ();

}

14. disable Client Verification

<% @ Page Language = "C #" clienttarget = downlevel %>

15. repeater, datalist, and DataGrid Control usage"

These controls simplify several common web application solutions, including reports, shopping carts, product lists, and queries.

Result and navigation menu. Repeater is the only control that allows HTML fragments to exist in its template.

16. Differences between server. Execute ("another. aspx") and server. Transfer ("another. aspx:

Execute transfers the execution from the current page to the specified page and returns the execution to the current page.

Transfer completely transfers the execution to the specified page

17. The XML file can either have its own architecture or exist in the *. XSL file, but the information must be specified in the root node of the XML file through the xmlns attribute, as shown below:

18. Reading XML files

Filestream myfs = newfilestream (server. mappath ("xmldtagrid. xml"), filemode. Open, fileaccess. Read );

Streamreader myreader = new streamreader (myfs );

Dataset myds = new dataset ();

Myds. readxml (myreader );

19. regularexpression_rvalidator

Symbol Meaning

^ Specify the start of the check

$ Specify the end Of the check

[] Check whether the input value matches one of the characters in square brackets.

\ W allows input of any value

\ D {} "\ D" indicates that the input value is a number, and {} indicates the number of occurrences of the specified data type.

+ Indicates that one or more elements will be added to the expression being checked

Example: email format (with @ number and ended with. com/. Net/. org/. edu)

Validationexpression_r = "^ [\ W-] + @ [\ W-] + \. (COM | net | org | EDU) $"

20. Important statements for data operations in the DataGrid Control:

Attribute: datakeyfield = "userid" // set userid as the primary key of the table. The value of this field cannot be updated to the database. It is best to set the primary key of the table as the primary key of the DataGrid.

Sqlcommand. parameters ["@ userid"]. value = DG. datakeys [(INT) E. item. itemindex]; // Number of Primary keys of the row to be updated (assign the primary key value of the currently selected row to a parameter of the command)

Sqlcommand. parameters ["@ fname"]. value = (textbox) E. item. cells [2]. controls [0]). text; // assign the modified row value to the parameter

21. Custom Controls:

A. User Control (the same as creating a page in ASP)

(I ). create a page, drag the control, and set properties/methods. the @ control command in <% @ controllanguage = "C #" DEBUG = "true" %> to define this page will contain the control code

(Ii) Save as *. ascx file, such as A. ascx.

(Iii). Use: Header <% @ register tagprefix = "myfirstcontrol" tagname = "mylbl" src = "A. axcs" %>

// Tagprefix is the control prefix, such as ASP in ASP: textbox.

// Tagname is used to specify the name of the custom control.

// SRC specifies the Control File Source

Body:

B. Use C # To create a custom control

(I). Create a pure code file, inherit the control class of the base class, and save it as *. CS, such as A. CS.

(Ii) Compile the code to generate the Assembly: CSC/T: Library/R: system. dll, system. Web. dlla. CS

// The Library tells C # That the compiler generates an assembly.

/// R: system. dll system. Web. dll tells C # That the compiler references the specified assembly.

(Iii). Place the generated DLL file in the bin directory.

(Iv). Usage: <% @ register tagprefix = "mine" namespace = "myowncontrols" assembly = "A" %>

22. Precautions for composite controls:

Public class mycompositin: control, inamingcontainer // inamingcontainer: if there are multiple control instances on the page, this interface can have a unique identifier for each {}/instance.

This. ensurechildcontrols (); // displays the child controls of the composite control on the page. This method checks whether the server control contains child controls.

Createchildcontrols

23. When will button/linkbutton/imagebutton/hyperlink be used?

1. The buttons and imagebutton are used to pass data back to the server.

2. hyperlink is used to navigate between pages

3. linkbutton is used to save data to the server or access the data on the server.

24. Tracking and debugging

Tracking:

1. Page-level tracking: The following page commands are included at the beginning of the page: <% @ page trace = "true" tracemode = "sortbycategory/sortbytime" %>

Custom message:

Trace. Write ("here is the string to be displayed ");

Trace. Warn ("here is the string to be displayed"); // It is the same as trace. Write, but the font is red.

Check whether a trail is used

Example: If (trace. isenabled) {trace. Warn ("enabled TRACE ")}

2. Application-level tracking: in the section of the web. config file

25. Set cache:

1. Output cache:

I. Page settings: Add <% @ outputcache duration = "120" varybyparam = "NONE" %> to the beginning of the page to be cached.

Note: The output content remains unchanged within two minutes after the page is requested.

Ii. Programming Method settings:

It mainly uses methods in the system. Web. httpcachepolicy class.

(1). response. cache. setexpires (datetime. Now. addseconds (120); // you must specify the expiration time in this method. For example, the current sentence is two minutes.

(2). response. cache. setexpires (datetime. Now. addseconds (120 ));

Response. cache. setslidingexpiration (true); // "adjustable expiration", mainly used in scenarios where the access volume is large but the access volume is balanced subsequently

Function: Set the cache expiration time In the first sentence, and enable the sliding expiration (adjustable expiration) in the second line ).

2. Data Cache:

(1). dataview mysource; (2). assign a value to mysource;

(3). cache ["mycache"] = mysource; (4). mysource = (dataview) cache ["mycache"]

26. Deployment: copy the statement directly to the product server: xcopy // xopy only accepts the physical path, not the virtual path

26. Custom page button

1.

Protected void itemcreated (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)

{

System. Web. UI. webcontrols. listitemtype elemtype = E. Item. itemtype;

If (elemtype = system. Web. UI. webcontrols. listitemtype. pager)

{
Tablecell pager = (tablecell) E. Item. controls [0];
For (INT I = 0; I {
Object o = pager. controls [I];
If (O is linkbutton)
{
Linkbutton H = (linkbutton) O;
H. Text = "" + H. Text + "";
}
Else
{
Label L = (Label) O;
L. Text = string. Format ("[page {0}]", L. Text );
}
}

}

}

2. Reference: zhangzs8896 (xiao'er)
Private void mydatagrid_itemcreated (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
Listitemtype lit_item = E. Item. itemtype;
Tablecelltc_item = (tablecell) E. Item. controls [0];

If (lit_item = listitemtype. pager)
{
For (INT I = 0; I {

Objectobj_item = tc_item.controls [I];

If (obj_item islinkbutton)

{
Linkbuttonlbn_item = (linkbutton) obj_item;
Lbn_item.text = lbn_item.text;
Lbn_item.font.size = 10;
Lbn_item.forecolor = color. fromname ("#666666 ");
Lbn_item.attributes.add ("onmouseover", "currentcolor = This. style. color; this. style. Color = '#14ac05 '");
Lbn_item.attributes.add ("onmouseout", "This. style. Color = currentcolor ");
}

Else
{
Labellbl_item = (Label) obj_item;
Lbl_item.forecolor = color. blue;
Lbl_item.font.bold = true;
Lbl_item.font.underline = true;
Lbl_item.text = "" + lbl_item.text + "";
Lbl_item.font.size = 10;
}
}
}
}

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.