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:
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