Learning ASP. Net from scratch-basics page 1/7

Source: Internet
Author: User
Tags servervariables

Day 1

Objective:

  • Measure the test taker's knowledge about the basic label, Textbox, And button controls.
  • Use the stringbuider class to connect strings
  • Understand the environment variables of the server

    Stringbuilder class:
    The namespace is system. Text.

    The stringbuilder class is an efficient class. The stringbuilder. append method to connect strings is very fast. It is used to connect a large number of strings, and its speed advantage will be reflected.

    Here are several examples:
    Add
    [C #] using system. text;
    [VB] imports system. Text

    [C #] stringbuilder sbfirst = new stringbuilder ();
    Sbfirst. append ("This is the first example of learning ASPnet </BR> ");
    Sbfirst. append ("this example is too simple </BR> ");
    Sbfirst. append .");
    Response. Write (sbfirst. tostring ());

    [VB] Dim sbfirst as stringbuilder = new stringbuilder ()
    Sbfirst. append ("This is the first example of learning ASPnet </BR> ")
    Sbfirst. append ("this example is too simple </BR> ")
    Sbfirst. append .")
    Response. Write (sbfirst. tostring)

    The following is the answer:
    Create a C # web application firstProgramProject. I will not talk about this nonsense.
    Put a button control: the ID is btnshowvariable
    Put a label control: Id labservervariable

    Add a button click event as follows:Code
    Private void btnshowvariable_click (Object sender, system. eventargs E)
    {
    Labservervariables. Text = "";

    Stringbuilder info = new stringbuilder ();

    Foreach (Object objvar in request. servervariables)
    {
    Info. append ("<span style =" font-size: 9pt "> ");
    Info. append (objvar. tostring ());
    Info. append ("= <font color = blue> ");
    Info. append (request. servervariables [objvar. tostring ()]);
    Info. append ("</font> </span> <br> ");
    }

    Labservervariables. Text = info. tostring ();
    }

    Result:

    The image is as follows:

    In this way, we can use
    Response. Write (request. servervariables ["remote_addr"]); // Ip address
    Response. Write ("<br> ");
    Response. Write (request. servervariables ["url"]); // Webpage URL

    Day 2

    Objective:

  • Understanding the usage of text boxes
  • Try... Catch... Syntax

    Today's content is very easy. Use an example to enter the year, month, and day to determine whether the input is correct.

    The image is as follows:

    Use a text box with IDs of txtyear, txtmonth, and txtdate;
    The code for the validation button is:
    Private void btncheck_click (Object sender, system. eventargs E)
    {
    Int year, month, date;

    // Convert the input characters to the int type,
    // An error will be triggered
    Try
    {
    Year = convert. toint32 (txtyear. Text );
    Month = convert. toint32 (txtmonth. Text );
    Date = convert. toint32 (txtdate. Text );
    }
    Catch
    {
    Labcheckinfo. Text = "the input is a non-numeric character. ";
    Return;
    }

    // If the first step passes the verification, convert the input number to the date format.
    // If the date format is invalid, an error is thrown.
    Try
    {
    Datetime dt = new datetime (year, month, date );
    }
    Catch
    {
    Labcheckinfo. Text = "the input number does not conform to the date format ";
    Return;
    }

    Labcheckinfo. Text = "the input is correct ";
    }

    Well, to add, about stringbuider
    Many people like to + = when processing strings. In fact, once defined, strings cannot be changed.
    The so-called + is just a new string variable created and assigned a value.
    Therefore, to use stringbuider's append method as much as possible, this will save a lot of server resources

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.