Asp. NET service-side basic controls Introduction

Source: Internet
Author: User
Tags form post

Asp. NET service-side basic controls Introduction

It is roughly divided into three types of controls:

HTML control, ASP. NET renders HTML controls as normal strings to the browser side without checking for correctness and cannot be processed on the server
Asp. NET Server-side controls, which are run on the server by ASP.
Renders to the client as an HTML control
Runat=server HTML controls, add runat= "Server" to HTML controls, also run on the server side, or operate in C # code
is also rendered as HTML, not as highly encapsulated as the ASP. NET server control, compared to the server-side controls:
This is handy if the control is not encapsulated by an ASP.

Asp. NET Server-side controls are the encapsulation of HTML and can be used with TXT in C # code. text= "abc" way to invoke, but was abused by someone,
Should use the best, service-side Control network system (OA), the background of the Internet system is not high frequency of access is very suitable for

Most of the ASP. NET is inherited from the Control,webcontrol class, and almost all of the members are:
1. ClientID, get the ID of the control on the client, the ID of the control on the service side does not necessarily equal the client ID, as the ListView is not
Therefore, it is best not to write the service-side ID if you want to operate on the client through Dom,jquery's getelementbyid,$ ("#id").
Instead of $ ("#<%=txt1.") Clientid%> ") or document.getElementById (' <%=button1.clientid%> ')
2. The Visible property, if the control is visible, if Visible=false is not rendered to HTML,
and the HTML style.display= ' None ' effect is not the same
3. CssClass property, the style name of the control, is the Class property of the control in HTML, can also modify the BackColor, and other properties separately,
However, this is not recommended because many inline styles are generated, which makes up the bandwidth and is inconvenient for uniform modification
4. Attributes, used to set the extra properties of the get control, as in Dom Setattribut (), Getattribut ()
button1.attributes["Code" = "5";//Add a code attribute to Button1 in HTML and assign a value of 5
button1.attributes["onmouseover"] = "alert (' hello! ')"; /Add a onmouseover Event

Asp. NET service-side basic controls

1. The lable control, the Text property is the displayed literal, is rendered by default to <span&gt, and when the Associatedcontrolid property is set
When used to associate a control, it is rendered as <Label> and the For property is set to the ClientID of the associated control
2. The literal control also shows a piece of text, but does not render extra tags, it is to show the value of the Text property
Another feature is the encode of the mode attribute, which can be used to escape code inside the text html,< will be converted into &lt; Prevent XSS Cross-site
3. TextBox control, TextBox control, TextMode property value Singleline,multiline,password, respectively rendered as:
Input (Type=text), textarea, and input (Type=password), when the AutoPostBack property is true,
The user focus left the TextBox will cause the page post submission, the implementation principle is TextChanged event, the text changed
When an event is triggered, ASP. NET submission form is best used to call the __doPostBack method
4. RadioButton control, rendered as input (Type=radio), grouped by GroupName property
5. Button control, OnClientClick property, render as input (Type=button) when user clicks on code executed in the browser
6. LinkButton control, rendered as a hyperlink, href is a piece of JavaScript code, the form post, unable to "open the connection in a new window"
7. ImageButton control, displayed as a picture, rendered as input (type=image)
Button,linkbutton,imagebutton and other controls have commandname,commandargument two properties and Command events
You can have multiple buttons share a Command event handler by reading the commandname,commandargument of the event object E
Two properties read the two parameters set on the Click button to perform different actions, such as editing, deleting multiple rows of data
8. Panel control, used to hold some controls, when the Visibe set to False, the inside of the controls are all hidden, default rendering into <DIV>
If you set the GroupingText property (set a border for the control and set the caption), it is rendered as a div tag that contains <fieldset>.
which is groupbox effect.
9. Hyperlink controls, hyperlinks, and LinkButton not the same (frequent), not to the server-side post, is a hyperlink,
NavigateURL: Link address, Text: Display text, if set ImageUrl the picture hyperlink will be displayed
The main advantage is that the virtual path is automatically converted to a client path, and after rendering it is a <a>
FileUpload control, File upload control, render as input (Type=file), properties:
Filecontent to get uploaded files in streaming form
HasFile bool value indicating whether the user chooses a file
filename, such as: a.jpg
PostedFile the file to be uploaded, also has the filename result as: C:\AA\a.jpg, can obtain the file detail information
The SaveAs method is used to save the file to the specified location on the disk, note: Only upload files of the specified type are allowed, the upload folder is not executed

Attention:
The default maximum upload file is only 4MB, if you want to larger, you need to modify the web.conf <system.web>, add the following properties:

11.multiview,view used as a simple guide, a MultiView containing multiple view,view must be inside the MultiView
ActiveViewIndex Current active view control, default = 1 not displayed, set starting from 0
Activeviewchanged event that is triggered when the view changes

The 12.Wizard Wizard is an upgraded version of MultiView, most of which are packaged

13.ImageMap hotspot map is mainly to deal with the image area, such as the map of China and other
ImageAlign Specify a picture address
Hotspots area settings, top, bottom, left, right
HotSpotMode The behavior of navigation, whether to cause postback

File upload case: FileUpload1 for upload control ID

if (fileupload1.hasfile)//Determine if the user has selected the file {    string path = Server.MapPath ("~/upload/");  Get Upload path    fileupload1.saveas (path + fileupload1.filename);//Save File    Response.Write ("<script>alert (' Upload success! ') </script> ");} else{    Response.Write ("<script>alert (' upload failed! ') </script> ");}

Perfect upload function:

    protected void Button1_Click (object sender, EventArgs e) {if (fileupload1.hasfile) {//Will            The virtual address is converted to a physical address string dirp = Server.MapPath ("~/upload/"); Determine if the directory exists and does not exist create if (!            Directory.Exists (DIRP)) {directory.createdirectory (DIRP); }//Gets the extension string extname = Path.getextension (fileupload1.filename).            ToLower (); Determine the uploaded file format if (extname! = ". jpg" && extname! = ". gif") {PAGE.CLIENTSCRIPT.R Egisterstartupscript (typeof (Page), NULL, "alert (' Format incorrect!")                ') ", true);            Return            }//through the GetFileName () method to get a file name that does not duplicate string filename = GetFileName () + extname;            Save the file with the SaveAs method, which must have the physical path Fileupload1.saveas (dirp + FileName);            Page.ClientScript.RegisterStartupScript (typeof (Page), NULL, "alert (' Upload succeeded! ')", true); Label1.Text = "uploaded file name:" + fileupload1.fiLename + "<br/>" + "File Size:" + FileUpload1.PostedFile.ContentLength + "<br/>" +  "File type:" + FileUpload1.PostedFile.ContentType + "<br/>" + "Save Path:"            + Dirp + FileName;        Image1.imageurl = ("~/upload/") + FileName; } else {Page.ClientScript.RegisterStartupScript (typeof (Page), NULL, "alert (' Please select File!        ') ", true);        }}//By obtaining the current time to get the non-duplicated file name public string GetFileName () {StringBuilder file = new StringBuilder (); File.        Append (DateTime.Now.Year); File.        Append (DateTime.Now.Month); File.        Append (DateTime.Now.Day); File.        Append (DateTime.Now.Second); File.        Append (DateTime.Now.Millisecond); Return file.    ToString (); }

Asp. NET service-side basic controls Introduction

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.