Past few days, the basic control of ASP. NET Server _ Repeater_ListView_ObjectDataSource & lt; Twenty-seven & gt ;,

Source: Internet
Author: User

Several days ago, the basic control of ASP. NET Server _ Repeater_ListView_ObjectDataSource <27>,

I. Server ID and Client ID

Generally, the control system that drag a server will automatically generate the ID of this control for us.

To receive the sending back data of the server control on the server, you must add AutoPostBack = "true" to the control"

Basic controls:

-- "The Server Control generates an ID, which can be accessed in the background based on this ID, and can also be obtained through the same ID on the page, but this ID is sometimes inconsistent, such

In <ItemTemplate>, different IDs are generated for each control cyclically. At this time, the client ID can be obtained through <% = TextBox1.ClientID %> on the page.

Document. getElementById ('<% = TextBox1.ClientID %>'). setAttribute ('style', 'background-color: red ');

Ii. Repeater

Bind data,

if (!IsPostBack)        {            TblPersonBLL bll = new TblPersonBLL();            this.Repeater1.DataSource = bll.GetAllDate();            this.Repeater1.DataBind();         }

-------- Header template:; table start (<table>) and header

<Asp: Repeater ID = "Repeater1" runat = "server" onitemcommand = "repeaterincluitemcommand"> <HeaderTemplate> <! -- Header: header template --> <table border = "0" cellpadding = "0" cellspacing = "0"> <tr> <th> NO. </th> <th> name </th> <th> age </th> <th> height </th> <th> gender </th> </tr> </HeaderTemplate>

---------- Item template: equivalent to a foreach loop. Each row in the loop table must have an ItemTemplate in a Repeater)

<ItemTemplate> <! -- Item template --> <tr onmouseover = "this. style. backgroundColor = 'cyn' "onmouseout =" this. style. backgroundColor = ''"> <td> <% # Eval ("AutoId") %> </td> <% # Eval ("UName ") %> </td> <% # Eval ("Age") %> </td> <% # Eval ("Height ") %> </td> <% # Eval ("Gender") %> </td> <asp: button Text = "delete" runat = "server" ID = "btnDel" CommandName = "AutoId" CommandArgument = '<% # Eval ("AutoId ") %> '/> </td> </tr> </ItemTemplate>

--------------- Split template: It will be separated by lines. For example, you can set the background color of each item in this template if you want to change the color of the line.

<AlternatingItemTemplate> <! -- Split the template --> <tr onmouseover = "this. style. backgroundColor = 'cyn' "onmouseout =" this. style. backgroundColor = ''"> <td> <% # Eval ("AutoId") %> </td> <% # Eval ("UName ") %> </td> <% # Eval ("Age") %> </td> <% # Eval ("Height ") %> </td> <% # Eval ("Gender") %> </td> <asp: button Text = "delete" runat = "server" ID = "btnDel" CommandName = "AutoId" CommandArgument = '<% # Eval ("AutoId ") %> '/> </td> </tr> </AlternatingItemTemplate>

----------------- Script Template: End of a table (</table>)

<FooterTemplate> <! -- Script Template -->



</Asp: Repeater>

Iii. ListView + ObjectDataSource

1) put the three layers first. It is best to use the object for the method parameters in bll.

Use ListView and ObjectDataSource together

2) set the data source of ListView to ObjectDatasource.

3) configure the data source of ObjectDataSource, and select the Business Object: BLL (business logic layer) -- "click Next

4) SELECT, UPDATE, INSERT, and DELETE are selected respectively.

-------- Custom page

1) Write a method to get the total number of records GetCount () to tell ObjectDataource the total number of records so that it knows the number of records to display.

/// <Summary> /// obtain the total number of records /// </summary> /// <returns> </returns> public object GetCount () {string SQL = "select count (*) from TblPerson"; return SqlHelper. executeScalar (SQL, CommandType. text, null );}

2) write another method to return the record of the specified number of pages so that you can know how to get the value of a specific range.

/// <Summary> /// efficient ListView paging /// </summary> /// <param name = "startRowIndex"> index of the Start row displayed on each page </param> /// <param name = "maximumRows"> How many rows of data are displayed on each page </param> /// <returns> </returns> public List <TblPerson> getPageNum (int startRowIndex, int maximumRows) {string SQL = "select * from (select row_number () over (order by autoId) ID, * from TblPerson) t where ID> @ startRowIndex and ID <= @ startRowIndex + @ maximumRows "; SqlParameter [] par = {new SqlParameter (" @ startRowIndex ", startRowIndex ), new SqlParameter ("@ maximumRows", maximumRows)}; DataTable dt = SqlHelper. getTable (SQL, CommandType. text, par); List <TblPerson> list = new List <TblPerson> (); if (dt. rows. count> 0) {foreach (DataRow item in dt. rows) {TblPerson per = RowToTblPerson (item); list. add (per) ;}return list ;}else {return null ;}}

3) When configuring the data source for objectDataSource, bind the method (GetPageNum) that returns the specified page number record to the SELECT method. Next --> complete

4) set the attributes of ObjectDatasource:

EnablePaging --> true

SelectCountMethod --> GetCount

MaximumRowParameterName --> maximumRows

StartRowIndexParameterName --> startRowIndex

The names of these two parameters must be the same as those of the GetPageNum () method.

5) Delete the two parameters generated in the source file.

<Asp: Parameter Name = "startRowIndex" Type = "Int32"/>

<Asp: Parameter Name = "maximumRows" Type = "Int32"/>

If the code of the page number is written outside the ListView, remember to bind the PagedControlId of DataPager to the corresponding ListView.

4. DorpDownList pay attention to sending back. Add AutoPostBack = "true" to the control; otherwise, the control data will not be sent back to the server and the server will not receive the data,

If (! IsPostBack) // judgment: If IsPostBack is a get request, false is returned; otherwise, true is returned. IspostBack is determined based on whether the hidden field of _ VIEWSTATE exists in the request message, if this hidden domain exists, it is regarded as post, because this hidden field has a server control on the page or the system will automatically generate such a hidden field and output it to the browser after you use the ViewState [Key] = value to customize it, this hidden field will be submitted only when the client submits the form. If you do not judge whether the request is a get request or a post request, the Code following it will be executed once.

--------- Bind data to the background class

If (! IsPostBack) // judge: If IsPostBack is a get request, false is returned; otherwise, true is returned, ispostBack is determined by the hidden domain _ VIEWSTATE in the request message. {TblPersonBLL bll = new TblPersonBLL (); this. dropDownList1.DataSource = bll. getAllDate (); // bind the data source this. dropDownList1.DataTextField = "UName"; // set the value displayed for each item. this value is an attribute of the returned data source entity. this. dropDownList1.DataValueField = "AutoId"; // set the value to be submitted to the server for each item. this value is an attribute of the returned data source object this. dropDownList1.DataBind (); // bind}

-------- Front-end data binding: AppendDataBoundItems = "true" must be added to append the data obtained from the database to the end"

<Asp: DropDownList ID = "DropDownList1" runat = "server" DataTextField = "UName" DataValueField = "AutoId" AppendDataBoundItems = "true"> <asp: listItem> select </asp: ListItem> </asp: DropDownList>

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: 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.