Develop programs based on JavaScript and ASP. NET web forms

Source: Internet
Author: User

ASP. NET is WebProgramDevelopment provides a new example. These include a series of server-based controls, which are similar to elements such as text boxes and buttons in HTML forms. The problem with using these controls is that the server must be called. Javascript provides multiple options for many tasks, and these processes do not need to call the server. Let's look at the powerful functions brought about by JavaScript and ASP. NET.

Performance is required
The Calling server requires bandwidth and server processing time. However, there will be no problem with a LAN program that can enjoy high-speed circulation, but it is different in the wide area network. The speed at which an Internet user connects depends on the user's dial-up Modem, bandwidth, or cable modem. You do not need to call the server when using the Javascript of your apartment.

Traditional Method
A standard webpage form includes a page such as the title box, body, and form. JavaScript Functions are traditionally placed in the title box of a webpage form. These functions are allowed to be loaded and used by other parts of the page. Once loaded, these functions are called from HTML elements.

Let's take a look at a simple JavaScript example:

1 Function Valsubmit () {
2 VaR Doc = Document. Forms [ 0 ];
3 Varmsg =   "" ;
4
5 If (Doc. firstname. Value =   "" ) {
6
7MSG+ = "-Please enter a first name. n";
8}
9
10 If (Doc. lastname. Value =   "" ) {
11MSG+ = "-Please enter a last name. n";
12}
13
14 If (Msg =   "" ) {
15Doc. Submit ();
16}   Else   {
17Alert ("The following errors were
18Encountered. nn" +MSG );
19}
20 }

This function will verify that the data is transmitted to two himl zones in the form. If one of the zones is empty, an error message is displayed and the operation stops. If the form is submitted when both zones have values, you can use the followingCodeCall this function:

1 <Input type = " Button " Value = " Submit " Name = " Btnsubmit " Onclick = " Valsubmit (); " >

With this relationship, the form will not be submitted before there is no data in the himl area. In this way, we can see that the Code becomes simpler, but it does not produce any side effects because no additional server is called. Javascript is short and practical, which means that the form does not require additional loading time.

Combine JavaScript with ASP. NET
ASP. NET web forms allow the use of standard HTML, so you can easily use the previous example. The ASP. Net user control allows you to easily call server code to process web forms. Fortunately, the combination of user controls and JavaScript is completely feasible. I will use an example of an ASP. NET button control to illustrate this process.

All the properties of the button control allow you to connect JavaScript to the control. First, place the JavaScript function in the ASP. NET webpage form, but you can change the placement method by adding the return value. If it is confirmed that the function value is true, the server functions related to the button will not be called. If the returned value is false, the form is not submitted.

1 <% @ Page Language = "C #" %>
2 <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
3 <HTML> 4 <Title> webform1 </title>
5 <Script language = "JavaScript">
6 Function valsubmit (){
7 VaR Doc = Document. Forms [0];
8 Varmsg = "";
9
10 If (Doc. firstname. value = ""){
11 MSG + = "-Please enter a first name. N ";
12 }
13 If (Doc. lastname. value = ""){
14 MSG + = "-Please enter a last name. N ";
15 }
16 If (MSG = ""){
17 Doc. Submit ();
18 Return true;
19 } Else {
20 Alert ("the following errors were encountered. nn" + MSG );
21 Return false;
22 }
23 }
24 </SCRIPT>
25 <Script language = "C #" runat = "server">
26 Private void btnsearch_click (Object sender, system. eventargs e ){
27 Response. Write ("Search ");
28 }
29 Private void page_load (Object sender, system. eventargs e ){
30 Btnsubmit. Attributes. Add ("onclick", "Return valsubmit ();");
31 }
32 </SCRIPT> 33 <Body>
34 <Form ID = "frmbuildertest" method = "Post" runat = "server">
35 <Label style = "Z-INDEX: 101; left: 10px; position: absolute; top: 48px"> First name: </label>
36 <Input style = "Z-INDEX: 102; left: 88px; position: absolute; top: 48px" type = "text" name = "firstname" id = "firstname">
37 <Label style = "Z-INDEX: 103; left: 10px; position: absolute; top: 88px"> last name: </label>
38 <Input style = "Z-INDEX: 104; left: 88px; position: absolute; top: 88px" type = "text" name = "lastname" id = "lastname">
39 <Br/>
40 <Asp: button id = "btnsubmit" style = "Z-INDEX: 105; left: 64px; position: absolute;
41 Top: 128px "runat =" server "text =" Submit "width =" 136px "onclick =" btnsearch_click "> </ASP: button>
42 </Form>
43 </Body>
44 </Html>

The most critical line in the Code is:

1 Btnsubmit. Attributes. Add ("onclick", "Return valsubmit ();");

You can use CSS to place elements in himl forms through style attributes. Form connects JavaScript Functions with ASP. NET buttons (btnsubmit) and HTML onclick events. When the form is submitted to the server, The onclick attribute in ASP. NET button will notify the system to call which functions.

If you are a VB. NET developer, you only need to change the C # module in the code. The corresponding VB. NET code is as follows:

1 <Script language = " VB " Runat = " Server " >
2 Private btnsearch_click (sender as object, e as system. eventargs)
3 Response. Write ( " Search " )
4 End sub
5 Private page_load (sender as object, e as system. eventargs)
6 Btnsubmit. Attributes. Add ( " Onclick " , " Return valsubmit (); " )
7 End sub
8 < / SCRIPT>

Powerful combination of JavaScript and ASP. NET

Javascript is a practical standard language for user web development. It works with ASP. the combination of Net web forms provides developers with a powerful tool set for building a solid program, and the stability of the program is considered a key component of the software performance.

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.