First, the Code 1.0 is not based on ASP. net is written using ASP. net leather bag ASP trap, rayinuk read 1.0 of the code is very confused, I believe many friends who have not read ASP and other directly read this code is very puzzled.
// Post content
Strresponse + = DT. Rows [I] ["msg_content"];
Strresponse + = "</TD> ";
Strresponse + = "</tr> ";
Strresponse + = "<tr> ";
Strresponse + = "<TD align = 'right'> ";
A friend who has used ASP for a period of time in such a writing method must be very clear. Compared with the table code, then, use the <% = XXXX %> output method to Display Dynamic Content much faster.
In fact, whether it is ASP or ASP. net or JSP. Finally, the browser only accepts html. the result we write here is to let the server explain it, then translate all our code into the HTML language, and finally the browser will explain HTML, result.
We put this code in page_load () Because ASP. NET will automatically execute this function. The name indicates that the function is executed when the page is loaded, so it can be displayed. By the way, events triggered by PAGE initialization are also automatically run and sorted. This will be explained through a special topic.
From the perspective of this informal ASP. NET program, it can indeed run, but let's look at how ASP. NET can make us better?
When there is a problem with our connection, the page must be an error. Of course, it is not a good thing to leak internal information in the program. Therefore, in the ASP era, we use an error object for processing, however. in the era of net, we can use try to process it.
Dbname = server. mappath (@ "data0621/database. mdb ");
// Create a connection for database connection
Oledbconnection conn = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + dbname );
// Open the connection
Conn. open ();
For the statements before conn. open (), there is certainly no error, but conn. open () is not. If the connection string to the database in connectionstring is incorrect, the error is indicated by Conn. open () is shown, which is not expected. Therefore, for errors that are prone to errors, we put it in try. If an error occurs, let's catch this exception, which will describe the error information and only tell the user that an error occurs on the server side. Because of HTML, this can not only prevent our web programs from getting stuck in one place, but also conceal internal error information, protecting the concealment of the program content. The detailed content will be described later.
For this Code:
// Post content
Strresponse + = DT. Rows [I] ["msg_content"];
Strresponse + = "</TD> ";
Strresponse + = "</tr> ";
Strresponse + = "<tr> ";
Strresponse + = "<TD align = 'right'> ";
We repeatedly use strresponse + = xxx in. net is not a good thing ,. net has prepared a stringbuilder class for us to solve this type of problem. The two are very different in efficiency. Of course, this is also explained in a special topic.
For the btnsubmit_click () event, the function we want to implement is to write the title and content into the database, which imitates the common
Rs. addnew
...
And then Rs. Update. In ASP, we use ADO, although. Net can also use ADO, but better ADO. Net can better serve us. Not many articles about ADO. Net can be clearly described, so we are not prepared to explain it deeply. What if we use Asp.net to make a guestbook better and faster? We will step into the ASP. NET Hall through the case of cold fengtian injury for us in version 2.0. Few codes help us implement the functions we want faster and better. We will immediately post the implementation method later.