ASP.Net/Asp Control/Repeater
Содержание
ItemTemplate in a repeater
<%@ Page %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn as New SqlConnection(strConnString)
Const strSQL as String = "SELECT * FROM authors"
Dim objCmd as New SqlCommand(strSQL, objConn)
Dim objDA as New SqlDataAdapter()
objDA.SelectCommand = objCmd
Dim objDS as New DataSet()
objDA.Fill(objDS)
objConn.Close()
rptAuthors.DataSource = objDS
rptAuthors.DataBind()
End Sub
</script>
<asp:repeater id="rptAuthors" runat="server">
<HeaderTemplate>
<table id="dlAuthors">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<b><%# DataBinder.Eval(Container.DataItem, "au_lname") %>,
<%# DataBinder.Eval(Container.DataItem, "au_fname") %></b>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
Repeater data binding
<script language="C#" runat="server">
public class State {
string _name;
string _timezone;
public State(string name, string timezone) {
_name = name;
_timezone = timezone;
}
public string Name {
get { return _name; }
}
public string TimeZone {
get { return _timezone; }
}
}
protected void Page_Load(object o, EventArgs e) {
if(!IsPostBack) {
ArrayList states = new ArrayList();
states.Add(new State("Washington", "Pacific"));
states.Add(new State("Utah", "Mountain"));
repeaterVertical.DataSource = states;
repeaterHorizontal.DataSource = states;
repeaterVertical.DataBind();
repeaterHorizontal.DataBind();
}
}
</script>
<form runat="server">
<asp:Repeater runat="server" id="repeaterVertical">
<HeaderTemplate>
<table><tr><th>State</th><th>TimeZone</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#((State)(Container.DataItem)).Name %></td>
<td><%#DataBinder.Eval(Container, "DataItem.TimeZone") %></td>
</tr>
</ItemTemplate>
<SeparatorTemplate><tr><td><hr></td></tr></SeparatorTemplate>
<AlternatingItemTemplate>
<tr>
<td><%#((State)(Container.DataItem)).Name %></td>
<td><%#DataBinder.Eval(Container, "DataItem.TimeZone") %></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
<asp:Repeater runat="server" id="repeaterHorizontal">
<HeaderTemplate><table><tr><th>State<br/>TimeZone</th>
</HeaderTemplate>
<ItemTemplate>
<td>
<%#DataBinder.Eval(Container.DataItem, "Name") %><br />
<%#DataBinder.Eval(Container.DataItem, "TimeZone") %>
</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td>
<%#DataBinder.Eval(Container.DataItem, "Name") %><br />
<%#DataBinder.Eval(Container.DataItem, "TimeZone") %>
</td>
</AlternatingItemTemplate>
<FooterTemplate></tr></table></FooterTemplate>
</asp:Repeater>
</form>
SelectedItemTemplate in repeater
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server" language="C#">
void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
void BindData()
{
const string strConnString = "server=localhost;uid=sa;pwd=;database=pubs";
SqlConnection objConn = new SqlConnection(strConnString);
const string strSQL = "SELECT * FROM titles";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);
objConn.Open();
dlTitles.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
dlTitles.DataBind();
objConn.Close();
}
void DisplayBookDetails(object sender, EventArgs e)
{
BindData();
}
</script>
<form runat="server">
<asp:DataList runat="server"
id="dlTitles"
OnSelectedIndexChanged="DisplayBookDetails"
Font-Name="Verdana"
Font-Size="11pt"
ItemStyle-BackColor="#eeeeee"
AlternatingItemStyle-BackColor="White"
HorizontalAlign="Center" CellPadding="8">
<ItemTemplate>
<i><%# DataBinder.Eval(Container.DataItem, "title") %></i>
[<asp:LinkButton
runat="server" CommandName="Select"
Text="View Book Details" />]
</ItemTemplate>
<SelectedItemTemplate>
<table>
<tr>
<td><b>Title:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "title") %></td>
</tr>
<tr>
<td><b>Type:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "type") %></td>
</tr>
<tr>
<td><b>Notes:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "notes") %></td>
</tr>
</table>
</SelectedItemTemplate>
</asp:DataList>
</form>
SeparatorTemplate in a repeater
<%@ Page Language="vb" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub
Sub BindData()
Const strConnString as String = "server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn as New SqlConnection(strConnString)
Const strSQL as String = "SELECT * FROM titles"
Dim objCmd as New SqlCommand(strSQL, objConn)
objConn.Open() "Open the connection
rptTitles.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
rptTitles.DataBind()
objConn.Close() "Close the connection
End Sub
Sub rptTitles_ItemCommand(sender As Object, e As RepeaterCommandEventArgs)
If e.rumandName = "Details" then
Dim lc as Label = e.Item.FindControl("lblTitleID")
Dim strTitleID as String = lc.Text
End If
End Sub
</script>
<form runat="server">
<asp:Repeater runat="server" id="rptTitles"
OnItemCommand="rptTitles_ItemCommand">
<ItemTemplate>
<b>Title:</b> <%# DataBinder.Eval(Container.DataItem, "title") %>
<br />
[<asp:LinkButton runat="server" id="btnDetails"
Text="View Book Details" CommandName="Details" />]
<asp:label id="lblTitleID" Visible="False" runat="Server"
Text="<%# DataBinder.Eval(Container.DataItem, "title_id") %>" />
</ItemTemplate>
<SeparatorTemplate>
<hr>
</SeparatorTemplate>
</asp:Repeater>
</form>