Import and read: A webform -- This section describes the basic concepts of ASP. NET webform and the changes to form items.
* A database application-how to use the DNS set in config. Web to connect to the database and some basic usage of database operation objects.
* Email sending-after introducing a system library (using the import identifier), you can use the msgmail object to conveniently operate the mail sending process.
* Upload -- <input type = file ...... Such an input item has the postedfile attribute in ASP. NET. Combined with the saveas event, you can upload the item.
--------------------------------------------------------------------------------
1. Use of webform
<Script language = "C #" runat = Server>
Void submitbtn_click (Object sender, eventargs e ){
Message. Text = "hi" + name. Text + ", you selected:" + category. selecteditem;
}
</SCRIPT>
The preceding is an event processing function written in C #. Void submitbtn_click (Object sender, eventargs e). Void indicates that the function has no return value, and sender indicates the object triggered in the event, here is the button object, and E is the parameter for triggering the event. The message, name, and category in this function are not defined. They are from the followingCode:
<Form action = "intro6.aspx" method = "Post" runat = "server">
<H3> name: <asp: textbox id = "name" runat = "server"/>
Category: <asp: dropdownlist id = "category" runat = Server>
<Asp: listitem> psychology </ASP: listitem>
<Asp: listitem> business </ASP: listitem>
<Asp: listitem> popular_comp </ASP: listitem>
</ASP: dropdownlist>
<Asp: button type = submit text = "lookup" onclick = "submitbtn_click" runat = "server"/>
<P>
<Asp: Label id = "message" runat = "server"/>
</Form>
First, we can note that the form writing method and HTML form are completely different. runat = server is added to all the form items, including the form itself. This attribute indicates that this is a server-side control item, and the original text input and drop-down menu have also changed. here we use:
<Asp: <control Category> id = [ID] property = runat = "server"/>
There are many types of controls (the list of all controls can be found in the appendix). The properties of each control are certainly different. This will be detailed in the future. In the code above, we can see the textbox, button ID, text attributes, and dropdownlist selecteditem attributes (Note: This attribute is only for C #. In VB, selecteditem is used. text attribute ).
Here, a server-side control <asp: Label id = "message" runat = "server"/> is displayed. This ASP: label is not available in traditional forms, it is a server-side text control, so there is a problem. If the traditional HTML does not have this element, how does ASP + receive it? Run this Program And then take a look at the HTML source code, you will find such a line:
<Input type = "hidden" name = "_ viewstate" value = "a0z-1715863018 _ x">
Yes, ASP + is passed in the form of a hidden form. In addition, after submission, you can find that the content entered in the text box and the selection of the drop-down menu are all preserved, which requires code implementation in ASP.
In ASP. NET, you can add server-side controls in another way:
<Script language = "C #" runat = Server>
Void addtext (){
Textbox text1 = new Textbox ();
Text1.text = "test ";
......
}
</SCRIPT>
This Code adds a Textbox Control. For more information, see ASP. NET webform.
2. Example of a Database Connection
Use DNS connection, code, description
Settings in config. Web:
<Deleetask>
<Add key = "myconn" value = "Server = localhost; uid = sa; Pwd = mypassword;
Database = somedatabase "/>
</Appsettings>
**************************************** ******************
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. SQL" %>
<Script language = "VB" runat = "server">
Sub page_load (SRC as object, e as eventargs)
Dim myconnection as sqlconnection
'Connection object of sqlserver
Dim config as hashtable
'Define a local variable
Config = context. getconfig ("etettings ")
'Use local variables to save the DSN connection string variable
Myconnection = new sqlconnection (config ("myconn "))
'Establish a connection
Dim mycommand as sqlcommand
'Declare a command object to insert data into the database
Dim parm1 as string = "sometextvalue"
Dim parm1 as string = "sometextvalue2"
Dim insertcmd as string = "insert into tablename values (@ parm1, @ parm2 )"
'Using the connection string
'Use the connection string
Mycommand = new sqlcommand (insertcmd, myconnection)
Mycommand. Parameters. Add (New sqlparameter ("@ parm1", sqldatatype. varchar, 50 ))
Mycommand. parameters ("@ parm1"). value = parm1
Mycommand. Parameters. Add (New sqlparameter ("@ parm2", sqldatatype. varchar, 50 ))
Mycommand. parameters ("@ parm2"). value = parm2
Mycommand. activeconnection. open ()
Mycommand. Execute ()
Mycommand. activeconnection. Close ()
End sub
</SCRIPT>
In the above Code. after the DNS set in the Web is connected, a command object is used to insert data. it is no different from ado, but the object name has changed.
3. Example of sending an email
Below is a piece of code, which can easily send an email with an attachment in HTML format. Unlike ASP, you need to write components for implementation.
<% @ Page Language = "C #" %>
<% @ Import namespace = "system. Web. util" %>
<%
Mailmessage msgmail = new mailmessage ();
Msgmail. To = "mail@mail.com ";
Msgmail. Cc = "mail1@mail.com ";
Msgmail. From = "webmaster@mail.com ";
Msgmail. Subject = "attachment ";
Msgmail. bodyformat = mailformat. html;
String strbody = "<B> here is an attachment </B> ";
Msgmail. Body = strbody;
Msgmail. attachments. Add (New mailattachment ("C: // temp // test.txt "));
Smtpmail. Send (msgmail );
Response. Write ("email has been sent successfully ");
%>
In the above Code, system is first introduced. web. util class library, so that you can call the mailmessage object. The use of its attributes is clear in the code above. Note that the bodyformat attribute is mailformat. when HTML is used, the mail body is in HTML format. If it is changed to mailformat. text, the format is text
Also, check the settings of your SMTP service. You need support for sending emails.
4. Upload example
You need to write components and code to upload files in ASP.
<HTML>
<Script language = "VB" runat = Server>
Sub uploadbtn_click (sender as object, e as eventargs)
Uploadfile. postedfile. saveas (server. mappath ("test.jpg "))
Myimage. imageurl = "test.jpg"
Myimage. Visible = true
End sub
</SCRIPT>
<Body>
<Form enctype = "multipart/form-Data" runat = Server>
<H3>
Select File to upload: <input id = "uploadfile" type = file runat = Server>
<Asp: button text = "upload me! "Onclick =" uploadbtn_click "runat = server/>
<HR>
<Asp: Image id = "myimage" visible = false runat = server/>
</Form>
</Body>
</Html>
Directly call the uploadfile object (its class library is ASP. NET by default, so you do not need to import a namespace .) It is easy to implement the upload. You don't have to compile the Upload Component yourself, as in ASP.