Some problems and solutions in using asp+

Source: Internet
Author: User
Tags format button type error handling insert processing text socket domain name mailmessage
Review:
In this article, tofu briefly talked about the problems we may encounter in asp+ programming and solutions to these problems, I believe that everyone in
After reading this article, the programming for asp+ will feel very
In this article, I mainly talk about some of the more intermediate applications, if you are still in the introductory stage for asp+, we recommend that you go to
1 How to use asp+ to get the submitted form element

Let's start with a very simple example to illustrate the benefits of the Server-side control that asp+ brings to us.
First we write a form for convenience, I am not in the description

Code:
--------------------------------------------------------------------------------

<script language= "VB" runat= "Server" > Sub Test (Sender as Object, Args as
EventArgs) ' Sender, Args These two parameters are the parameters of the Click event Response.Write ("Your name is: '" &
Txtname.value & "'.")      Response.Write ("The email you filled out is: '" & Txtemail.value & "'.") End
Sub </SCRIPT> <form method=post> <table align = center> <tr><td> your name said: <input
Type= "text" id= "txtname" runat= "Server" > </td></tr> <tr><td> your email is: <i Nput
Type= "text" id= "Txtemail" runat= "Server" > </td></tr> <tr><td> <input typ E= "Submit"
Value= "Submit" runat= "Server" onserverclick= "Test" > </td></tr> </table> < /form>

--------------------------------------------------------------------------------


So we write the program is like a VB frm the same (it is said that in the VB 7.0 will indeed increase the function)
Some people will also say that they really want to maintain continuity with the previous ASP program, it doesn't matter if you don't like to use
Txtname.value This kind of grammar, still can use Request.Form (txtname), how, cool enough.

2. Processing databases in asp+

Asp+ introduced a lot of server-side database components, just as we did in VB's database control Dblist,dbgrid
If you have used the RDS component in VI, you must have a general understanding of this stuff that Ms has long wanted to launch, but
Asp+ is asp+ not Asp,ms does have a big bang on ASP

Code:
--------------------------------------------------------------------------------

<%@ Page language= "VB"%> <%@ import namespace= "System.Data"%> <%@ Import
Namespace= "System.Data.ADO"%> <script language= "VB" runat= "Server" > Sub search_onclick (Sender as
Object, E as EventArgs) ' The following statement tells us that in asp+ we can finally give up the variant of this inefficient data class
Type Dim connobj As ADOConnection Dim dtscmdobj As Adodatasetcommand Dim dtsobj As
DataSet Dim Str_sql As String Dim conn_str As String Str_sql = "SELECT * FROM
table_name "CONN_STR =" dbq= "& Server.MapPath (" Database.mdb ") &_";D River={microsoft
Access Driver (*.mdb)}; " Connobj = new ADOConnection (conn_str) dtscmdobj = new Adodatasetcommand
(Str_sql, connobj) dtsobj = New DataSet () dtscmdob.filldataset (dtsobj, "test")
Dtgrid. DataSource = Dtsobj. Tables ("Test"). DefaultView Dtgrid. DataBind () End
Sub</script>id= "Query" text= "Queries" runat= "server"/></form><asp:datagrid id= "Dtgrid"
Headerstyle-font-bold= "True" tooltip= "data Grid provided with asp+" runat= "Server"
Maintainviewstate= "false"/></body>
--------------------------------------------------------------------------------


Did you see it? This is not VB is what??? Some friends may be under VB is very not accustomed to VB data binding, yes, asp+ also provides a call
Set of Datasets (Object)
We've just seen it, too.
We can not use the Bdatabind () method, write it directly with Response.Write, let ' s look!

Code:
--------------------------------------------------------------------------------

<% @import namespace= "System.data.SQL"%> <script language= "vb" runat= "Server" > Sub displaydata ()
Dim dtreader As SqlDataReader Dim sqlcmd As SQLCommand Dim SQL Server As String Dim
String SQL Server = "Server=my.sql.database;uid=userid;password=password;" SQL = "SELECT * from"
table_name "SQLCMD = New SQLCommand (sql,sqlserver) sqlcmd. Activeconnection.open ()
Sqlcmd.execute (Dtreader) Response.Write ("After the database open again, we'll show you the number of fields in this table
Value ") while Dbread.read () Response.Write (dtreader). Item ("Test_column") & "<br>") End While
End Sub</script>

--------------------------------------------------------------------------------

We've seen how to read the values in the database from the database, let's look at an example to see how
That modifies the contents of the database.



Code:
--------------------------------------------------------------------------------

<%@ import namespace= "System.Data"%> <%@ import namespace= "System.Data.SQL"%> <HTML> <BODY>
<script language= "VB" runat= "Server" > Sub Page_Load (Src as Object, E as
EventArgs) ' This event triggers the Dim myconnection as when the page loads
SqlConnection myconnection = new SqlConnection ("Server=sqlserver; Uid=sa; pwd=;
Database=test ") Dim mycommand As SQLCommand Dim sql As String
Sql= "INSERT into Test (Testcol) VALUES (' 1234 ')" ' sql= ' update test set
Testcol= ' 1234 ' "' sql=" delete from test "mycommand = new SQLCommand (" INSERT into Test
(Testcol) VALUES (' 1234 ') ", MyConnection) Myconnection.open ()
Mycommand.execute () Myconnection.close () Response.Write ("Update data into
Work ") End Sub </script> </BODY> </HTML>

--------------------------------------------------------------------------------


After the database is done, let's see how we can do some of the features that asp+
3. Upload, send email and operate socket
In the previous article, I have lifted the upload and operation socket implementation domain Name query Complete example, where we mainly look at
See Using ASP to send email

Code:
--------------------------------------------------------------------------------

<% @Page language= "C #"%> <% @Import namespace= "System.Web.Util"%> <% "mailmessage" MAILMSG = new
MailMessage ();  mailmsg.to = "roboo@21cn.com";  Receiver of the letter Mailmsg.from = "roboo@21cn.com"; Of
Letter Person Mailmsg.subject = "This is a test letter sent with asp+";  Mailmsg.bodyformat = Mailformat.text; Description
The format of the letter is text format, we can of course set mailformat.html Mailmsg.body = "with Attachment";
MAILMSG.ATTACHMENTS.ADD (New MailAttachment ("C:\\test.bmp"));   This is the attachment smtpmail.send (MAILMSG); //
Letter Response.Write ("Mail sent successfully");%>

--------------------------------------------------------------------------------



4. How do I do error handling in asp+?
On Error Resume Next is our only way of dealing with ASP, what is it now in asp+?

Simply put, as long as we add such a sentence to the front of the page, the problem is solved.
<% @page errorpage = "/error.aspx"%>
When the error occurs, we just need to unify the processing in the error.aspx file
5 in asp+ processing text files, in the ASP we use FileSystemObject very convenient to process text files, asp+ in this respect continue
Promote the more convenient


Code:
--------------------------------------------------------------------------------

<%@ Import namespace= "System.IO"%> </title> ReadFile = File.OpenText ("C:\sample.txt") do Strin = ReadFile. ReadLine () Response.Write (
Strin & "<br>") Loop Until Strin = Null ReadFile. Close%></body>
--------------------------------------------------------------------------------

is not very simple, let's take a look at the writing text file


Code:
--------------------------------------------------------------------------------

<%@ Import namespace= "System.IO"%> ("C:\sample.txt") mywriter.writeline ("This is a text file generated with asp+") mywriter.writeline ("Bean
Rot making, are fine ") Mywriter.close Response.Write (" Test.txt Create and write success! ") %>
</body>
--------------------------------------------------------------------------------



Finally finished, through this article we have been able to according to the knowledge involved in the article, materials and code can be developed
A medium difficult asp+ site, of course, I am here just a few simple examples and explanations, specifically in the process of development
People still have to rely on their own efforts.

If you miss PHP, then there is because JSP is too difficult, do not give up asp+ efforts, friends


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.