ASP. NET Study Notes 2

Source: Internet
Author: User
10. About JavaScript
-1. Use hotkeys

<Body onkeydown = "if (event. keyCode = 13) query_data ()">

0. Call the JS pop-up dialog box

Page. Response. Write ("<script language = javascript> alert ('wrong password! '); Window. history. back (-1); </script> ");

Page. Response. End ();

1. To call a JS program, add "()", for example, checkinput ()

2. The code snippet for calling JS in HTML is as follows:

<Script language = "javascript">

Function closeWnd ()

{

Window. close ();

}

</Script>

<Input type = "button" value = 'close 'onclick = 'closewnd () '>

3. usage of the Document Object: If your form (named Form1) has a control (named Keyword), the reference method is Document. form1.Keyword. value.

4. For related examples, see the test_js.sln project.

5. Use status bar:

<A href = "tpage.htm" onMouseOver = "window. status = 'Just another stupid link... '; return true">

Input type = "button" name = "look" value = "? "Onclick =" statbar ('This is the status bar.

(Statusbar )! '); ">

<Input type = "button" name = "erase" value = "d)" onclick = "statbar (''); ">

6. Example of date used:

<Script language = "LiveScript">

<! -- Hiding

Today = new Date ()

Document. write ("? (?? /", Today. getHours (),": ", today. getMinutes ())

Document. write ("<br> ?) ? -: ", Today. getMonth () + 1,"/", today. getDate (),"/", today. getYear ());

// End hiding contents -->

</Script>

7. Generate a random number

<Script language = "LiveScript">

Function RandomNumber (){

Today = new Date ();

Num = Math. abs (Math. sin (today. getTime ()));

Return num;

}

</Script>

8. open a new window:

<Script language = "javascript">

<! --

Window. open ('page.html ', 'newwindow', 'height = 100, width = 400, top = 0, left = 0, toolbar = no, menubar = no, scrollbars = no, resizable = no, location = n o, status = no') // write a line

-->

</SCRIPT>

Parameter description:
<Script language = "javascript"> the js SCRIPT starts;

Window. open command to pop up a new window;

'Page.html 'name of the pop-up window;

'Newwindow' indicates the name of the pop-up window (not the file name). It is optional and can be replaced by null;

Height = 100 window height;

Width = 400 window width;

Top = the pixel value between the 0 window and the top of the screen;

Left = 0 the pixel value between the window and the left of the screen;

Toolbar = no indicates whether to display the toolbar. yes indicates display;

Menubar and scrollbars indicate the menu bar and scroll bar.

Resizable = no: whether to change the window size. yes: yes;

Location = no indicates whether the address bar is displayed. yes indicates yes;

Status = no whether to display the information in the status bar (usually the file has been opened), yes is allowed;

</SCRIPT> end of js SCRIPT

9. automatically close the window

<Script language = "JavaScript">

Function closeit ()

{

SetTimeout ("self. close ()", 10000) // millisecond

}

</Script>

10. Open Window Cookies

In retrospect, although the pop-up window above is cool, there is a little bit of a problem (immersed in joy, do not find it ?) For example, if you place the above script in a page that requires frequent access (such as the homepage), the window will pop up every time you refresh the page, isn't it very annoying? :-(

Is there a solution? Yes! Follow me. We can use cookies to control it.

First, add the following code to the <HEAD> area of the HTML homepage:

<Script>

Function openwin (){

Window. open ("page.html", "", "width = 200, height = 200 ")

}

Function get_cookie (Name)

{

Var search = Name + "="

Var returnvalue = "";

If (document. cookie. length> 0)

{

Offset = document. cookie. indexOf (search)

If (offset! =-1)

{

Offset + = search. length

End = document. cookie. indexOf (";", offset );

If (end =-1)

End = document. cookie. length;

Returnvalue = unescape (document. cookie. substring (offset, end ))

}

}

Return returnvalue;

}

Function loadpopup ()

{

If (get_cookie ('popped') = '')

{

Openwin ()

Document. cookie = "popped = yes"

}

}

</Script>

Then, use <body onload = "loadpopup ()"> (not openwin, but loadpop !) Replace the original <BODY> sentence on the home page. You can refresh the page or re-enter the page. The window will no longer pop up. Real Pop-Only-Once!

The creation and application skills of the pop-up window are basically completed!

11. Move forward and backward and locate

<Html>

<Body>

<Form name = "buttonbar">

<Input type = "button" VALUE = "Back" onClick = "history. back ()">

<Input type = "button" VALUE = "JS-Home" onClick = "locationpolic'script.html '">

<Input type = "button" VALUE = "Next" onCLick = "history. forward ()">

</FORM>

</Body>

</Html>

12. Complete Cookies

<Script language = "JavaScript">

<! --

Var bVisitedToday = false;

Var lastVisit = GetCookie ("lastVisit ");

If (lastVisit! = Null)

{

LastVisit = 1 * lastVisit;

Var lastHere = new Date (lastVisit );

Var rightNow = new Date ();

If (lastHere. getYear () = rightNow. getYear ()

& Amp; lastHere. getMonth () = rightNow. getMonth ()

& Amp; lastHere. getDate () = rightNow. getDate ())

{

BVisitedToday = true;

}

}

If (bVisitedToday = false)

{

SetLastlastVisitCookie ();

Window. location = "http://www.thehungersite.com /"

}

Else

{

// Window. location = "about: blank"

}

Function getCookieVal (offset)

{

Var endstr = document. cookie. indexOf (";", offset );

If (endstr =-1)

Endstr = document. cookie. length;

Return unescape (document. cookie. substring (offset, endstr ));

}

Function GetCookie (name)

{

Var arg = name + "= ";

Var alen = arg. length;

Var clen = document. cookie. length;

Var I = 0;

While (I <clen ){

Var j = I + alen;

If (document. cookie. substring (I, j) = arg)

Return getCookieVal (j );

I = document. cookie. indexOf ("", I) + 1;

If (I = 0) break;

}

Return null;

}

Function SetCookie (name, value)

{

Var argv = SetCookie. arguments;

Var argc = SetCookie. arguments. length;

Var expires = (argc> 2 )? Argv [2]: null;

Var path = (argc> 3 )? Argv [3]: null;

Var domain = (argc> 4 )? Argv [4]: null;

Var secure = (argc> 5 )? Argv [5]: false;

Document. cookie = name + "=" + escape (value) +

(Expires = null )? "": ("; Expires =" + expires. toGMTString () +

(Path = null )? "": ("; Path =" + path) +

(Domain = null )? "": ("; Domain =" + domain) +

(Secure = true )? "; Secure ":"");

}

Function setLastlastVisitCookie ()

{

Var rightNow = new Date ();

Var expdate = new Date ();

Expdate. setTime (expdate. getTime () + 1 * (24*60*60*1000); // + 1 day

SetCookie ("lastVisit", rightNow. getTime (), expdate ,"/");

}

Function ResetCookie ()

{

SetCookie ("lastVisit", 0, null ,"/");

}

// -->

</Script>

11. Refreshing pages in Asp.net
In asp, data is often committed to a hidden framework to achieve no refreshing. In asp.net

You can implement it with a simple setting.

Void Page_Load (Object sender, EventArgs e)

{

This. SmartNavigation = true;

}

Or write it directly

<% @ Page language = "c #" Codebehind = "AllBook. aspx. cs" SmartNavigation = true AutoEventWireup = "false" Inherits = "wx. AllBook" %>

12. Asp Performance Optimization
Some time ago, performance was optimized for an old system due to work needs.

The environment is described as follows:

1. The system is based on information. The presentation layer is implemented using asp, and the database uses MS SqlServer 2000.

2. asp code is messy with many errors. As errors are blocked, the system can barely use them.

3. All data access is directly implemented by writing a hybrid code in the asp program, and repeatedly accessing the same table in the same file.

Based on the above situation, after analysis, determine the principles,

1. Write the processing and access logic of the database to the stored procedure.

2. Asp only displays the output.

3. Fix the original bug.

4. Create an index and change the SQL statements for data access. Note the following:

1) The Where condition must prefix the index field condition.

2) count (*) is not allowed. Replace it with count (field1.

3) select * cannot be replaced by select field1, field2...

4) use exits instead of IN.

5) for single Insert Update and delete statements, transactions are not used and the table is locked by transactions, which affects the efficiency of concurrency. In some special cases

Force not lock table

With (nolock)

13. about passing values (hiding code)
1. In the HTML of the first Page, confirm that the Inherits attribute is added to the @ Page command:

<% @ Page language = "c #" Inherits = "test_js.page1" %>

2. Create a Public attribute on the first page.

Public string getName

{

Get

{

Return txtNumber. Text;

}

}

3. Add a transfer command as follows:

Server. Transfer ("edit. aspx ");

4. Set HTML on the second page as follows:

<% @ Reference Page = "page1.aspx" %>

5. declare the first variable

Public page1 fp1;

6. Reference Method

If (! This. IsPostBack)

{

Fp1 = (page1) Context. Handler;

TxtRc. Text = fp1.getName;

}

// This. IsPostBack method is used to test whether the page is returned.

7. Complete!

14. Application events
<Script language = "C #" runat = "server">

Void Application_OnBeginRequest (Object sender, EventArgs E)

{

}
Void Application_OnEndRequest (Object sender, EventArgs E)

{
}

</Script>

Session events
Void Session_OnStart ()

{

Session ["username"] = ""; // initialization parameters

}

Void Session_OnEnd ()

{

}

16. Open the page in this window
Onclick = "window. open ('? Addnew = true', '_ self ','')"

17. Data Binding to DropDownList
Drp_Tech.DataSource = ds. Tables ["staff table"]. DefaultView;

Drp_Tech.DataTextField = "technician ";

Drp_Tech.DataBind ();

18. Table usage
1. You can use width = 100% to fill the Table with the width of the form.

2. You can use <td colspan = "3" align = "center"> & nbsp; </td> to merge columns. rowspan and Span are used to merge rows.

19. DropDownList
DropDownList2.Items. FindByText ("2"). Selected = true;

20. Remove Multiple Borders in HTML (overall)
Leftmargin = "0" topmargin = "0" marginwidth = "0" marginheight = "0"

21. Download with Java
<Script language = "javascript">

Var strDownloadUrl = "http://g.china.msn.com/7MEZH_CN/ZH-CHS/SETUPDL.EXE ";

Function LaunchDownload () {location. href = strDownloadUrl ;}

</Script>

22. Scroll words:
<Marquee scrolldelay = "180"> welcome to Haitian customer relationship management system! </Marquee>

Layer 23
You can use a layer in Login. aspx to solve the location problem.

<Div id = "Layer1" style = "BORDER-RIGHT: #000000 0px; BORDER-TOP: #000000 0px; LEFT: 28%; OVERFLOW: hidden; BORDER-LEFT: #000000 0px; WIDTH: 432px; BORDER-BOTTOM: #000000 0px; POSITION: absolute; TOP: 24%; HEIGHT: 282px; visibility: visible; "> </div>

24. XML
DataSet dsXml = new DataSet ("Settings ");

String filepath = Server. MapPath ("\ xml \ set. xml ");

DsXml. ReadXml (filepath );

DataGrid1.DataSource = dsXml;

Response. write ("<br> the first connection is:" + dsXml. tables ["Set"]. rows [0] ["ConnectionString1"]. toString ());

DataGrid1.DataMember = "Set ";

DataGrid1.DataBind ();

25. process input parameters
Http://mov.hzgwbn.com/movie.asp? Addnew = true

Add the following to void Form_Load:

String _ addnew = Request. QueryString ["addnew"];

26. About _ dopostback ()
Look at this. It may be useful !!

<SCRIPT language = "JavaScript">

<! -- Hide from older browsers

Function GetInput ()

{

Input = prompt ('input value that your want to send server :','');

If (input) & (input! = ""))

{

Document. forms ['form1 ']. elements ['hidden1']. value = input;

_ DoPostBack ('button1 ','');

}

}

// Stop hiding -->

</SCRIPT>

The _ DoPostBack client function is used to stimulate server events.

2. To complete this complete page, we need the following Form

<Form id = "Form1" method = "post" runat = "server">

<INPUT id = "Hidden1" type = "hidden" name = "Hidden1" runat = "server">

<Asp: Button id = "Button1" runat = "server" Text = "Button"> </asp: Button>

<Asp: TextBox id = "TextBox1" runat = "server"> </asp: TextBox>

</Form>

Pay attention to the details. These are all running on the server (runat = server)

3. The code on the server side (in the cs file) is followed. The Page_load function enables the Server Control Button1 to have The onclick Event code when presented to the client as follows:

Private void Page_Load (object sender, System. EventArgs e)

{

// Place user code here to initialize the page

This. Button1.Attributes. Add ("onclick", "GetInput ();");

}

4. Finally, it is the button event. The Code is as follows. It obtains a value entered by the user and displays the value in the TextBox. The Code is as follows:

Private void button#click (object sender, System. EventArgs e ){

String str = this. Hidden1.Value;

This. TextBox1.Text = str;

}

5. My own example

Onclick = "javascript: if (! Confirm ('OK') {return };__ doPostBack ('button3 ','');"

6. Note: The page must contain a LinkButton or other controls that can have _ doPostBack.

27. Toolbar
1. Processing with Javascript

<Script language = "javascript">

Function WitchB ()

{If (event. srcNode! = Null)

{

Var bt = event. srcNode. getAttribute ("id ");

Switch (bt)

{

Case "C1 ":

Aa ();

Break;

Case "C2 ":

Here we call the process in. aspx. cs. How to Write this sentence? Use _ dopostback ()????

Break;

}

}

}

Function aa ()

{...}

</Script>

<Body onload = "Toolbar1.onbuttonclick = WitchB;" MS_POSITIONING = "GridLayout">

2. Process in C #

Private void Toolbar1_ButtonClick (object sender, EventArgs e)

{

Response. Write (sender. ToString ());

TextBox1.Text = sender. ToString ();

If (sender. ToString () = "ToolbarButton-button1 ")

{

Response. Redirect ("http: // localhost/htcrm ");

}

}

 

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.