Repeater, like the GridView data list, are used to display the information of the database, where repeater is the most basic form of the list, and its usage is more flexible.
First, the location of repeater:
Toolbox-Data-repeater
Drag and drop the display after entering the page:
Switching to Source view will reveal that he has only two lines of code:
<asp:repeater id= "Repeater1" runat= "Server" >
</asp:Repeater>
Other formats, etc., need to be implemented in code.
Second, Repeater basic list format:
<Asp:repeaterID= "Repeater1"runat= "Server"> <HeaderTemplate><!--Label for header list - <Table> <TR> <TD>Names</TD> <TD>Zip</TD> <TD>Management</TD> <TD>Delete</TD> </TR> </HeaderTemplate> <ItemTemplate><!--Table Subject content label - <TR> <TD></TD> <TD></TD> <TD></TD> <TD></TD> </TR> </ItemTemplate> <FooterTemplate><!--table's Foot label - </Table> </FooterTemplate> </Asp:repeater>
The table header needs to be written to
Main data on <itemtemplate> ... The </ItemTemplate> tag, which is the display of the table's main data content, requires only one row, and the system automatically generates additional rows after binding the data.
The tail <footertemplate> of the table; </FooterTemplate> tags, tail notes, etc.
Third, the repeater data binding.
You need to specify the column name of the bound table in the HTML page first
(You also need to create a LINQ to SQL class to connect to the database, with a database table diagram, just a similar table)
Use the <% #Eval ("column name")%> The format is as follows (only two columns are bound here):
<ItemTemplate> <TR> <TD><%#Eval("Name") %></TD> <TD><%#Eval("Postcode") %></TD> <TD></TD> <TD></TD> </TR> </ItemTemplate>
Displayed in Design view after a successful binding:
The code in CS:
PrivateDiqudatacontext Diqu;//externally defined LINQ for ease of use protected voidPage_Load (Objectsender, EventArgs e) {Diqu=NewDiqudatacontext ();//Initializing LINQ if(!IsPostBack) //First time load {Binddata (); } } //creates a method of data binding for easy use Public voidBinddata () {//querying data by criteria varque = fromMinchDiqu. MemberswhereM.parentid.tostring (). Length = =2 SelectM//determine the provincial area by the number of characters//binding DataRepeater1.datasource =que; Repeater1.databind (); }
Binding Complete:
Iv. Edit and Delete buttons for repeater
Add the LinkButton control to the appropriate location in the table, and then set CommandName (set first name for the control) CommandArgument (bound data, index)
<ItemTemplate> <TR> <TD><%#Eval("Name") %></TD> <TD><%#Eval("Postcode") %></TD> <TD> <Asp:linkbuttonID= "Linkbutton_guanli"CommandArgument= ' <% #Eval ("ParentID")%>' Commandname= ' Xiaji "runat=" Server > Subordinate Management</Asp:linkbutton> </TD> <TD> <Asp:linkbuttonID= "Linkbutton_shanchu"CommandArgument= ' <% #Eval ("ParentID")%>' Commandname= ' Shanchu "runat=" Server > Delete</Asp:linkbutton> </TD> </TR> </ItemTemplate>
In the properties of the Repeater control-event-itemcommand (triggered when you click Repeater any button), double-click the auto-Generate event
The code in CS:
protected voidRepeater1_itemcommand (Objectsource, RepeaterCommandEventArgs E) { //Delete if(E.commandname = ="Shanchu")//if you clicked the Delete button{//The data type of the ParentID column in the table is int intCode =int. Parse (E.commandargument.tostring ()); Members CDATA= Diqu. Members.single (r = R.parentid = =code); Diqu. Members.deleteonsubmit (CDATA); Diqu. SubmitChanges (); Binddata (); } //Show Subordinates if(E.commandname = ="Xiaji") { intCou = E.commandargument.tostring (). Length;//gets the string length to determine the level of the provincial and municipal regions if(Cou = =2)//two-digit province or municipality{Repeater1.datasource=NULL;//Clear Data stringCode =e.commandargument.tostring (); //match by character length and intercept string (the location of the search is City) varXia = fromMinchDiqu. MemberswhereM.parentid.tostring (). Length = =4 whereM.parentid.tostring (). Substring (0,2) = = CodeSelectm; Repeater1.datasource=Xia; Repeater1.databind (); } if(Cou = =4)//the city is 4-digit{Repeater1.datasource=NULL;//Clear Data stringCode =e.commandargument.tostring (); //match by character length and intercept string (the location of the search is region) varXia = fromMinchDiqu. MemberswhereM.parentid.tostring (). Length = =6 whereM.parentid.tostring (). Substring (0,4) = = CodeSelectm; Repeater1.datasource=Xia; Repeater1.databind (); } if(Cou = =6)//region is 6 digits, minimum level { //Pop-up tip informationResponse.Write ("<script>alert (' reached the minimum level! ');</script>"); } } }
20150301-asp. NET of repeater