. Net2.0 implements deletion in Repeater

Source: Internet
Author: User

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.

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.