Although repeater controls are not as commonly used as gridview controls, repeater needs to be used in some specific scenarios. In fact, in net2.0, formview controls can also achieve effects similar to repeater, in addition, all data is displayed through templates.
However, deleting a control in the Repeater control is not as easy as that in the gridview. The following is an example:
Default. aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "repeater_default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: sqldatasource id = "sqldatasource1" runat = "server" connectionstring = "<% $ connectionstrings: northwindconnectionstring %>"
Selectcommand = "select * from [region]"> </ASP: sqldatasource>
</Div>
<Asp: repeater id = "repeater1" runat = "server" performanceid = "sqlperformance1" onitemcommand = "repeaterperformanitemcommand" onitemcreated = "repeater=itemcreated">
<Itemtemplate>
<Table border = "1">
<Tr>
<TD>
<Asp: Label text = '<% # eval ("regionid") %>' runat = "server" id = "label1"> </ASP: Label>
<Asp: Label text = '<% # eval ("regiondescription") %> 'runat = "server"> </ASP: Label>
<Asp: button text = "delete" runat = "server" id = "button1"/>
</TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: repeater>
</Form>
</Body>
</Html>
Default. aspx. CS:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class repeater_default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void repeaterincluitemcommand (Object source, repeatercommandeventargs E)
{
Switch (button) E. Item. findcontrol ("button1"). Text)
{
Case "delete ":
Sqlconnection conn = new sqlconnection ("Server =.; database = northwind; uid = sa; Pwd = sa ;");
Conn. open ();
Sqlcommand comm = new sqlcommand ("delete from region where regionid = '" + (Label) E. item. findcontrol ("label1 ")). text + "'", Conn );
Comm. executenonquery ();
Conn. Close ();
Response. Write (Label) E. Item. findcontrol ("label1"). Text );
Break;
}
}
Protected void repeaterincluitemcreated (Object sender, repeateritemeventargs E)
{
For (INT I = 0; I <this. repeater1.items. Count; I ++)
{
(Button) This. repeater1.items [I]. findcontrol ("button1"). Attributes. Add ("onclick", "alert ('ff ')");
}
}
}
This allows deletion.