Asp. NET Server-side controls (class0617)

Source: Internet
Author: User

Asp. NET service-side basic controls Introduction

Asp. NET Server-side controls are ASP. HTML, which can be used txt1 in C # code. text= ' abc ' This way to modify the value of input, ASP. NET turns the server-side control into HTML code output to the browser. The server-side control is a very attractive beginner, very easy to get started, and is also the most criticized things. The use of the service-side control of the network system, the background of the Internet system, and other areas of low frequency access is still very suitable.

(Internet company, product Company (OA)) a property written in the label of a server-side control is output to the client if it is not a property built into the control

Most of the ASP. NET is inherited from the control, WebControl class, and almost all of the members are:

(1) ClientID, the ID of the control on the client, the ID of the control on the server does not necessarily equal the ID in the client HTML, such as in a template for a control such as a ListView. So if you want to manipulate the control on the client through the JavaScript Dom, jquery's getElementById, $ ("#id"), it's best not to write the service-side ID directly, but $ (' #<%=txt1. Clientid%> '). Use the jquery event to set the different styles for the mouse to move over the control and away from the control. You can see the difference between the ClientID and the ID in the user control. UserControl, Master, ListView. Recommended Forever with ClientID

(2) Visible property, whether the control is visible, if Visible=false is not going to the HTML, this and in HTML to the element 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. You can also modify properties such as BackColor, BorderStyle, and so on, but this is not recommended because there are a lot of inline styles that generate large HTML sizes and are not easy to modify uniformly. (

4) Attributes, which is used to set the extra properties of the get control. Is the same as setattribute (), getattribute () in the DOM. button1.attributes["A1"] = "2.jpg"; HTML events appear on the server as attributes button1.attributes["onmouseover"] = "alert (' Hello ')";

Additional properties for server-side controls

Description: All server-side controls can use not only the properties defined by the control, but also additional properties, including HTML attributes that the control does not encapsulate (such as browser-side event pages such as onmouseover as attributes), ASP. NET renders its unrecognized properties to the client intact. Additional properties can also be set through the Attributes property in code:

checkbox1.attributes["onmouseover"] = "alert (' Hello ')";

Asp. NET service-side basic controls 1

1. Label control. The Text property is a display literal. The Associatedcontrolid property is used to associate a control that, if empty, is displayed as a span, and if specified as the ID of a control, it is displayed as a <Label> in an HTML and set the For property to the ClientID of the associated control.

2. The literal control also shows a piece of text, but the literal control does not render any extra tags, but it shows the value of the Text property.

3. TextBox control, text box control. The TextMode property values Singleline, MultiLine, Password, respectively, are rendered as input (Type=text), textarea, and input (Type=password). When the AutoPostBack property is true, the user focus leaves the textbox, which causes the page post, and the implementation principle is the AutoPostBack of the ASP. TextChanged event (requires Autopostback=true to be modified after the trigger, otherwise the page commits to trigger), the text changes when the event triggers. Asp. NET, it is best to call the __doPostBack method when submitting a form.

4. RadioButton control, corresponding to input (Type=radio), grouped by groupname attribute

Asp. NET service-side basic control Button

5, Button control. OnClientClick property, when the user clicks the button on the browser execution code, note that OnClientClick is a string attribute, the code is written in JavaScript code, run on the browser side. <asp:button id= "Btndel" runat= "Server" onclientclick= "return confirm (' Do you really want to delete it? ') "text=" Delete "/>

6, LinkButton, usage and button is similar, the difference is that the button control renders as buttons, and LinkButton renders as hyperlinks. Do not use LinkButton to implement ordinary hyperlinks, because the LinkButton href is a piece of JavaScript code, the form of the post, do not "open the connection in a new window." And say "row delete" in that example, the href is the same as the JavaScript hyperlink principle. Generally with the button on the line, only the user asked "to become a hyperlink good" to adopt LinkButton, is a browser rendered as a hyperlink, the server as a button to use things. Hyperlink:ispostback=false, no form content submitted, no onclick event.

7, ImageButton control is similar to button, but is displayed as a picture, rendering as input (Type=image)

8, Button, LinkButton, ImageButton and other controls have CommandName, commandargument two properties and command events, you can have more than one button control to share a command event handler function, The two parameters set on the Click button are read by reading the CommandName, commandargument two properties of the event object E to perform a different operation. Example: editing and deleting multiple rows of data. This usage is most used in controls such as ListView.

Asp. NET service-side basic Controls 3

9. The Panel control is used to put some controls in place. If you set the GroupingText property then it will be rendered as a div tag with <fieldset>, which is groupbox effect, otherwise rendered as <div>

10, Hyperlink controls, hyperlinks. Not the same as LinkButton (frequent test), not to the server-side post, is a hyperlink. NavigateURL: the link address; text: Displays the texts. If you set the ImageUrl property, a picture hyperlink is displayed.

11, FileUpload control, File upload control. Render as input (Type=file). Properties: Filecontent The uploaded file as a stream; filename upload file name; HasFile bool value, indicating whether the user chooses a file, the SaveAs method is used to save the file to the specified location on disk. Vulnerability: File Upload Vulnerability (upload an aspx to download the source code, Process.Start start formatting, create an administrator, turn on Remote Desktop). Workaround: Only allow upload of the specified type of file, upload folder does not give execute permission.

Three types of controls

HTML control, ASP. NET renders HTML controls as normal strings to the browser side, without checking for correctness and failing to process on the server side.

Asp. NET Server-side control, a control that has been highly encapsulated in ASP., is simple to use and runs on the servers side, and can be manipulated using C # code on the service side, rendering it to the client as an HTML control.

The Runat=server HTML control. Adding runat= "Server" on top of the HTML control is also run on the server side, or it can be manipulated by the service side using C # code and rendered to the client, not as highly encapsulated as the ASP. NET service-side controls, and the exposed properties are mostly ordinary HTML attributes. The benefit compared to the ASP. Runat=server HTML control is handy when you need to manipulate the control on the server side, if the control is not encapsulated by the ASP. runat= The server's HTML control also handles virtual paths (~/), Id→clientid, so Onmouseover= "document.getElementById (') may be used in the use of virtual paths, UserControl. <%=TextBox1.ClientID%> '). value= ' haha '; "There is a problem directly in the property that will output the <% directly to the browser side, so do not write <%%> in the property value of the control.

When a server control uses a path, there is a ~ sign in the middle that represents the root directory that is returned to the current Web site application.

Run=server the benefits of the control, the golden mean!

1, unlike the ASP. NET server control so the weight, package so much, the programmer can better control the quality of the generated content. Easier for artists to use, because the artist does not recognize <asp:textbox

2, relative to the HTML control: 1) can manipulate the server code in the control 2) to automatically handle the virtual path ~ 3) to handle the ClientID. Order of selection: Html→run=server html→asp.net service-side control

HTML control (runat=server) corresponds to server type

A→htmlanchor;form→htmlform;head→htmlhead;input→htmlinputbutton, HtmlInputCheckBox, HtmlInputText, etc. meta→HtmlMeta ; Table→htmltable;tr→htmltablerow;td→htmltablecell;title→htmltitle. Labels that are not individually encapsulated (such as DIV) or custom labels (such as MMM) correspond to the type HtmlGenericControl. Use the Attributes property to manipulate an unpackaged property.

Instead of remembering it alone, forget it, get a label in ASPX and try it out.

The server-side HTML control is not as advanced as the ASP. NET control, such as the bgcolor property of the ASP. HTML control, which is a string type and the bgcolor property

Introduction to data-bound controls

Data binding is divided into two parts, data source and data-bound control, data-bound control through the data source to obtain data, through the data source to isolate the data provider and data consumer , data-bound control through the data source to modify the data source has SqlDataSource, AccessDataSource, ObjectDataSource, LinqDataSource, EntityDataSource, XmlDataSource (SiteMapDataSource is a sitemap-specific data source), because most projects do not directly connect the database (because it violates the most basic layering principle), so SqlDataSource, AccessDataSource is not used, LinqDataSource, EntityDataSource is also only used in very extreme projects that use LINQ, EF, and XmlDataSource is the process of working with XML data. ObjectDataSource is the most widely used data source in web development, and it can easily be used for database switching.

Data-bound controls have list data-bound controls (DropDownList, RadioButtonList, ListBox, CheckBoxList, BulletedList, and so on) and

Complex controls (DataGrid, GridView, DetailsView, FormView,ListView, Repeater, DataList, GridView, and so on are all ListView subsets).

The DataGrid is deprecated in complex controls, Repeater is the most lightweight component, with the most in the front of the Internet, and the ListView is the GridView, DetailsView, FormView, Repeater, DataList, the unification of those controls, the benefits of those controls are all in the ListView, and the ListView controls are used, so the data-bound controls are primarily about list data-bound controls ,Repeater , and ListView, the project will use the FormView, GridView.

ObjectDataSource

ObjectDataSource is used to make a class A data source, the TypeName property is the full name of the data source class, DeleteMethod (Delete method), InsertMethod (new method), SelectMethod (Query method), UpdateMethod (Modify method) and several other properties, respectively, the method name of the class to delete, insert, query, update data, these methods may have parameters, the value of the parameter is through DeleteParameters, UpdateParameters, Set by nested nodes such as InsertParameters.

ObjectDataSource accesses the ordinary class written by himself and marks it on the class [DataObject] DataObjectMethod

Hand-written ObjectDataSource is too cumbersome to be done using a visual interface. You can configure the ObjectDataSource by dragging and dropping it onto the interface and selecting "Configure Data Source" on the smart flag in the upper-right corner. Data source class General Typenameadapter class, select the class, select Next, respectively, select the corresponding method to get, delete, UPDATE, insert data.

List-bound controls

DropDownList shows the data from ObjectDataSource, select the data source (DataSourceID property) for just the ObjectDataSource, and set the Display field (DataTextField) and the Value field (DataValueField). RadioButtonList, ListBox, CheckBoxList, BulletedList and so on are also used.

Manually set the binding, in addition to data binding (recommended) for setting a data source for the control's DataSourceID property, you can also set the binding in code by code (the old version of ASP. NET is only so bound, not recommended in the new version).

Code binding allows you to bind any object that implements the IEnumerable interface to a data-bound control.

Listbox2.datasource = new object[]{3,5,6};

Listbox2.databind ();

Because data-bound controls save data in ViewState by default, the data will not reload every time the page is refreshed, only the first time it needs to be loaded (! IsPostBack)

Because code binding has a lot of trouble with disabling ViewState, it is recommended to use DataSourceID to determine whether the data should be re-acquired.

DropDownList the original "Please select Gender" and the coexistence of data binding items: Appenddatabounditems= "true" to add data-bound items to the back.

Because data-bound controls save data in ViewState by default, the data will not reload every time the page is refreshed, only the first time it needs to be loaded (! IsPostBack)

protected void Page_Load (object sender, EventArgs e)

{

if (! IsPostBack)

{

Listbox2.datasource = new object[] {3, 5, 6};

Listbox2.databind ();

}

}

Complex data-bound controls

In addition to the simple list data-bound controls that display text, value, there are more complex data-bound controls, such as displaying people information on the interface, including names, ages, photos, and so on. This is the time to use Repeater, ListView and other controls.

Repeater (today's focus)

Repeater (foreach) is used to iterate through the data in the bound data source, and the format of each data is determined by the <ItemTemplate> of Repeater, and the template is displayed multiple times, like foreach, The equivalent of the statement in {} in ItemTemplate. <ItemTemplate> Name: <% #Eval ("name")%><b> Ages: <% #Eval ("Age")%></b><br/></ Itemtemplate>. Note:there cannot be spaces in the middle of% and # . The <% #Eval ("Name")%> indicates that the Name property of the current entity object is displayed in this location, noting that the data binding method called Eval, bind is used #.

Because Eval is a property displayed to the specified location, it can also be displayed in the text box <ItemTemplate> name: <asp:textbox runat= "Server" text= ' <% #Eval ("name")% > '/></itemtemplate>

Be careful not to write text= "<% #Eval (' Name ')%>" because <%%> is in C # code, ' is a character, and not a string can also be used in server controls <asp:textbox text= ' <% #Eval ("Name")%> ' runat= "Server" ></asp:TextBox>

Exercise: Select row highlighting with jquery (Put Repeater in Table tab, ItemTemplate is TR)

Build strongly Typed DataSet

Don't forget to build successfully, put ObjectDataSource, set bindings

Repeater binding to ObjectDataSource

Set ItemTemplate

Repeater other templates

1, <alternatingitemtemplate>, set the different display style of interlaced, if set <alternatingitemtemplate>, then the odd line with <ItemTemplate> template, Even lines <AlternatingItemTemplate> templates

<alternatingitemtemplate><asp:textbox backcolor= "Red" id= "TextBox2" text= ' <% #Eval ("Name")%> ' runat= "Server"/></alternatingitemtemplate>. Setting an interlaced color is to prevent too much data from viewing the serial.

2, HeaderTemplate, FooterTemplate: Head, tail of the template, respectively, displayed in front of all data and behind.

3, SeparatorTemplate: Delimiter between two data, such as line break

Case

Case: Show people information, name, age, photos (the site is the picture path 1, the ItemTemplate inside with the server control: 1) The bound property must be enclosed in single quotes myname= ' <% #Eval ("Name")%> '. HTML controls are either single or double-quoted. 2) The value of the attribute is either an ordinary value, or the entire binding expression, cannot be mixed, such as Imageurl= ' images/<% #Eval ("Imgpath")%> '. The HTML control does not have this problem. It is not possible to do so without a server-side control <%# "img/" +eval ("APic")%> exercise: Displays a list of articles. ID, title, author, link address. If the title is too long, only the first 10 words are displayed and the ellipsis (without the style implementation) do not write overly complex C # code in ASPX, if the code is more complex, try to extract to the CS practice: Display Links list, fields: Site name, hyperlink, link type (text, picture), Logo image address. Text links, photo links show

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.