Asp. NET Learning article (3)--a few simple asp.ent examples

Source: Internet
Author: User
Tags format object button type html form sql net variables mailmessage

A webform--here will introduce the basic concepts of asp.net webform, as well as the changes in the table items.

* Application of a database-how to use the DNS connection database set in Config.web, some basic usages of database Operation objects.

*email send--After the introduction of a System class library (with the import identifier), using the Msgmail object, you can easily operate the mail delivery process.

* Upload--<input Type=file ... Such an input item has postedfile attribute in asp.net, combine SaveAs event, can realize upload.

First, the use of WebForm

The following are the referenced contents:

<script language= "C #" runat=server>

void SubmitBtn_Click (Object sender, EventArgs e) {
Message.Text = "Hi" + Name.text + ", you selected:" + Category.selecteditem;
}
</script>

The above is an event handler that is written in C #, void SubmitBtn_Click (object sender, EventArgs e), void means that the function has no return value, sender is the object that is triggered in the event, and here is the button object, E is the parameter that triggers the event. The message, Name, and category in the function are not defined, and they come from the following code:

The following are the referenced contents:
<form action= "intro6.aspx" method= "POST" 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>

The first thing you can notice is that the form is written differently from the HTML form. All of the form items include the form itself followed by the Runat=server, which indicates that this is a server-side control, the original text input, Drop-down menu, and so on the writing has changed. Here are the following:

<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), and the properties of each control are certainly different, which will be described in more detail in the code above, where we can see the textbox, the button ID, the Text property, and DropDownList SelectedItem attributes (Note: This property is only for C #, in VB, to use the Selecteditem.text attribute as the corresponding substitution).

There is a server-side control <asp:label id= "message" runat= "Server"/>, this asp:label is not the traditional form, it is a server-side text control, then there is a problem, If there is no such element in the traditional HTML, then how does asp+ receive it? You run this program, and then look at the HTML source code, you will find this line:

<input type= "hidden" name= "__viewstate" value= "a0z-1715863018__x" >

Yes, asp+ is passing through this hidden form. In addition, after submitting you can find in the text box to fill in the content, as well as the Drop-down menu options are preserved, which is required in the ASP code to implement.

One point that needs to be raised is that in asp.net there is another way to add server-side controls:

The following are the referenced contents:
<script language= "C #" runat=server>
void AddText () {
TextBox text1=new textbox ();
Text1. text= "TEST";
......
}
</script>

This section of code adds a TextBox control. For more information, see: What is ASP.net's WebForm

Examples of second database connections

Use DNS connection, code, description

CONFIG. Settings in the WEB:

The following are the referenced contents:

<appsettings>
<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
' SQL Server's Connection object
Dim Config as HashTable
' Define a local variable
Config = Context.getconfig ("appsettings")
' Use local variables to save DSN connection string variables
MyConnection = New SqlConnection (Config ("MyConn"))
' Establish a connection
Dim MyCommand as SQLCommand
' Declare a Command object to insert data from 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 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 code above, the insert data was completed with a command object after the connection was made using DNS set in Config.web. It's nothing different from the previous ADO, but the name of the object has changed.

三、一个 example of sending mail

Here is a piece of code that is very easy to send an email with an HTML format and an attachment. And not like the ASP in the need to write their own components to implement.

The following are the referenced contents:
<% @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 a 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 code above, The first is to introduce the System.Web.Util class library, in order to invoke the MailMessage object, the use of its properties, the above code has been very clear, to note that there are BodyFormat property in the mailformat.html, the message body is HTML format, if you change to M Ailformat.text, then text format, and, please check your SMTP service settings, mail sent requires its support.

Examples of 四、一个 uploads

ASP upload files need to write their own components, code, description

The following are the referenced contents:

<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>
Select File to Upload: <input id= "UploadFile" Type=file runat=server>
<asp:button text= "Upload me!" onclick= "Uploadbtn_click" runat=server/>

<asp:image id= "MyImage" Visible=false runat=server/>

</form>
</body>

Call the UploadFile object directly (its class library is asp.net default, so there is no additional Import for a Namespace.) It is easy to upload, not like in the ASP, to write their own upload components.



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.