The examples in this article describe the use of repeater controls in asp.net. Share to everyone for your reference. The implementation methods are as follows:
Repeater binding Data:
Copy Code code as follows:
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:
Copy Code code as follows:
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:
Copy Code code as follows:
<%@ 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>
<FOOTERTEMPLATE><HR/></footertemplate>
</asp:Repeater>--%>
<div>
<asp:repeater id= "Repeater1" runat= "Server"
onitemcommand= "Repeater1_itemcommand" >
<HeaderTemplate><table>
<TR><TD style= "width:100px" > Number </td><td style= "width:100px" > Name </td>
<TD style= "width:100px" > Pictures </td><td> </td><td> </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= ' editor ' commandargument= ' <%# ' The 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= ' editor ' commandargument= ' <%# ' The Eval (" Sid ")%> ' ></asp:LinkButton></td>
</tr>
</AlternatingItemTemplate>
<SeparatorTemplate>
<TR><TD colspan= "5" ></SeparatorTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
Enable, disable:
Copy Code code as follows:
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 status= @status 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> </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>
I hope this article will help you with the ASP.net program design.