ASP. NET--use of the Repeater control

Source: Internet
Author: User
Tags connectionstrings

Reprinted from the network-original URL "http://blog.csdn.net/haitaodoit/article/details/7021214"

Repeater binding Data:

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
Bindstudent ();
}

private void Bindstudent ()
{
String str = configurationmanager.connectionstrings["STUCNN"]. ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection (str))
{
using (SqlDataAdapter da = new SqlDataAdapter ("SELECT * from Student", SQLCNN))
{
DataSet ds = new DataSet ();
Da. Fill (DS);
This. Repeater1.datasource = ds;
This. Repeater1.databind ();
}
}
}

Delete data:

protected void Repeater1_itemcommand (object source, RepeaterCommandEventArgs e)
{
if (E.commandname = = "Delete")
{
String str = configurationmanager.connectionstrings["STUCNN"]. ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection (str))
{
using (SqlCommand SQLCMM = Sqlcnn.createcommand ())
{
Sqlcnn.open ();
Sqlcmm.commandtext = "Delete from student where sid="
+ e.commandargument.tostring ();
Sqlcmm.executenonquery ();
}
}
This. Bindstudent ();
}
else if (E.commandname = = "Edit")
{
Server.Transfer ("edit.aspx?sid=" + e.commandargument.tostring ());
}
}

Front desk:

<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<%--<asp:repeater id= "Repeater1" runat= "Server" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate><div>
<asp:label id= "Lblsid" runat= "server" text= ' <%# Eval ("Sid")%> ' ></asp:Label>
<asp:label id= "lblsname" runat= ' server ' text= ' <%# Eval ("sname")%> ' ></asp:Label>
<asp:image id= "Imgphoto" runat= "server" Imageurl= ' <%# Eval ("photo")%> '/></div>
</ItemTemplate>
<SeparatorTemplate>
</SeparatorTemplate>
<AlternatingItemTemplate>
<div style= "
<asp:label id= "Lblsid" runat= "server" text= ' <%# Eval ("Sid")%> ' ></asp:Label>
<asp:label id= "lblsname" runat= ' server ' text= ' <%# Eval ("sname")%> ' ></asp:Label>
<asp:image id= "Imgphoto" runat= "server" Imageurl= ' <%# Eval ("photo")%> '/></div>
</AlternatingItemTemplate>
&LT;FOOTERTEMPLATE&GT;&LT;HR/></footertemplate>
</asp:Repeater>--%>
<div>

<asp:repeater id= "Repeater1" runat= "Server"
onitemcommand= "Repeater1_itemcommand" >
<HeaderTemplate><table>
&LT;TR&GT;&LT;TD style= "width:100px" > Numbering </td><td style= "width:100px" > Name </td>
&LT;TD style= "width:100px" > Pictures </td><td>&nbsp;</td><td>&nbsp;</td></ Tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# eval ("Sid")%></td><td><%# eval ("sname")%></td>
<td> '/></td>
<td><asp:linkbutton id= "Btndelete" runat= "server" text= "delete" commandname= ' delete ' commandargument= ' <%# Eval ("Sid")%> ' ></asp:LinkButton></td>
<td><asp:linkbutton id= "Btnedit" runat= "Server" text= "edit" commandname= ' edit ' commandargument= ' <%# Eval (" Sid ")%> ' ></asp:LinkButton></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style= "
<td><%# eval ("Sid")%></td><td><%# eval ("sname")%></td>
<td> '/></td>
<td><asp:linkbutton id= "Btndelete" runat= "server" text= "delete" commandname= ' delete ' commandargument= ' <%# Eval ("Sid")%> ' ></asp:LinkButton></td>
<td><asp:linkbutton id= "Btnedit" runat= "Server" text= "edit" commandname= ' edit ' commandargument= ' <%# Eval (" Sid ")%> ' ></asp:LinkButton></td>
</tr>
</AlternatingItemTemplate>
<SeparatorTemplate>
&LT;TR&GT;&LT;TD colspan= "5" ></SeparatorTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>

</div>
</form>
</body>

Enable, disable:

protected void Repeater1_itemcommand (object source, RepeaterCommandEventArgs e)
{
string status = E.commandname;
if ((E.commandname = = "true") | | (E.commandname = = "false"))
{
String str = configurationmanager.connectionstrings["STUCNN"]. ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection (str))
{
using (SqlCommand SQLCMM = Sqlcnn.createcommand ())
{
Sqlcnn.open ();
Sqlcmm.commandtext = "Update student set[email protected]where sid= "
+ e.commandargument.tostring ();
SqlCmm.Parameters.AddWithValue ("@status", e.commandname);
Sqlcmm.executenonquery ();
}
}
This. Bindstudent ();
}
}

<%@ page language= "C #" autoeventwireup= "true" codefile= "Default2.aspx.cs" debug= "true" inherits= "DEFAULT2"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:repeater id= "Repeater1" runat= "Server"
onitemcommand= "Repeater1_itemcommand" >
<HeaderTemplate><table><tr><th> number </th><th> name </th><th> status </th ><th>&nbsp;</th></tr></HeaderTemplate>
<ItemTemplate>
<tr><td><%# Eval ("SID")%></td>
<td><%# Eval ("sname")%></td>
<td><%# Convert.toboolean (Eval ("status"))? " Enable ":" Disable "%></td>
<td><asp:linkbutton id= "Btnsetstatus" runat= "server" commandargument= ' Eval ("Sid") ' text= ' <%# Convert.toboolean (Eval ("status"))? " Disable ":" Enable "%> ' Commandname= ' <%# Convert.toboolean (Eval (" status "))?" False ":" True "%> ' ></asp:LinkButton></td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>

ASP. NET--use of the Repeater control

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.