控件之Repeater控件使用,repeater控件的使用

歡迎大家來討論,订正,一定虛心接纳。

转发自网络-原网站【

 

repeater绑定数据:

1.為什麼使用Repeater控件?

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

  關於把從數據庫讀取的數據綁定到前台頁面,笔者們能够行使DataGrid、DataGridView以至Repeater來佈局,三者均能够间接綁定DataTable中的字段,然而對於特殊的报表,如供给跨行或跨列顯示的數據,則供给重畫风华正茂個table,然後再綁定數據。

private void BindStudent()
{
string str =
ConfigurationManager.ConnectionStrings[“stucnn”].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection
{
using (SqlDataAdapter da = new SqlDataAdapter(“select * from student”,
sqlCnn))
{
DataSet ds = new DataSet();
da.Fill;
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
}
}
}

永利澳门游戏网站,2.Repeater控件之好處

除去数据:

  小编們能够率性的定義Table的表頭、表身和表尾。

protected void Repeater1_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
if (e.CommandName == “Delete”)
{
string str =
ConfigurationManager.ConnectionStrings[“stucnn”].ConnectionString;
using (SqlConnection sqlCnn = new SqlConnection
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand
{
sqlCnn.Open();
sqlCmm.CommandText = “delete from student where sEdit”)
{
Server.Transfer(“Edit.aspx?sC#” AutoEventWireup=”true”
CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

3.Repeater控件一些基礎設定

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;

  例:以下例子展示了什么样對Repeater控件進行綁定,并實現在table中實現編輯和换代數據行的操作

<html xmlns=”;
<head runat=”server”>
<title></title>
</head>
<body>
<form runat=”server”>
<%–<asp:Repeater ID=”Repeater1″ runat=”server”>
<HeaderTemplate>
<hr />
</HeaderTemplate>
<ItemTemplate><div>
<asp:Label ID=”lblSid” runat=”server” Text='<%# Eval
%>’></asp:Label>
<asp:Label ID=”lblSname” runat=’server’ Text='<%# Eval
%>’></asp:Label>
<asp:Image ID=”imgPhoto” runat=”server” ImageUrl='<%# Eval %>’
/></div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
<AlternatingItemTemplate>
<div style=”
<asp:Label ID=”lblSid” runat=”server” Text='<%# Eval
%>’></asp:Label>
<asp:Label ID=”lblSname” runat=’server’ Text='<%# Eval
%>’></asp:Label>
<asp:Image ID=”imgPhoto” runat=”server” ImageUrl='<%# Eval %>’
/></div>
</AlternatingItemTemplate>
<FooterTemplate><hr /></FooterTemplate>
</asp:Repeater>–%>
<div>

html端代碼:

<asp:Repeater ID=”Repeater1″ runat=”server”
onitemcommand=”Repeater1_ItemCommand”>
<HeaderTemplate><table>
<tr><td style=”width:100px”>编号</td><td
style=”width:100px”>姓名</td>
<td
style=”width:100px”>图片</td><td> </td><td> </td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval %></td><td><%# Eval
%></td>
<td><img width=”60px” height=”60px” src='<%# “images/” +
Eval %>’ /></td>
<td><asp:LinkButton ID=”btnDelete” runat=”server” Text=”删除”
CommandName=’Delete’ CommandArgument='<%# Eval
%>’></asp:LinkButton></td>
<td><asp:LinkButton ID=”btnEdit” runat=”server” Text=”编辑”
CommandName=’Edit’ CommandArgument='<%# Eval
%>’></asp:LinkButton></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style=”
<td><%# Eval %></td><td><%# Eval
%></td>
<td><img width=”60px” height=”60px” src='<%# “images/” +
Eval %>’ /></td>
<td><asp:LinkButton ID=”btnDelete” runat=”server” Text=”删除”
CommandName=’Delete’ CommandArgument='<%# Eval
%>’></asp:LinkButton></td>
<td><asp:LinkButton ID=”btnEdit” runat=”server” Text=”编辑”
CommandName=’Edit’ CommandArgument='<%# Eval
%>’></asp:LinkButton></td>
</tr>
</AlternatingItemTemplate>
<SeparatorTemplate>
<tr><td colspan=”5″><hr /></td></tr>
</SeparatorTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>

<asp:Repeater runat=”server” ID=”reportRepeater”
OnItemCommand=”reportRepeater_ItemCommand”
OnItemDataBound=”reportRepeater_ItemDataBound”>
<HeaderTemplate>

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

<table>

启用,禁用:

<tr><th></th>

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
{
using (SqlCommand sqlCmm = sqlCnn.CreateCommand
{
sqlCnn.Open();
sqlCmm.CommandText = “update student setstatus=@statuswhere
s@status”,e.CommandName);
sqlCmm.ExecuteNonQuery();
}
}
this.BindStudent();
}
}

<th>用戶ID</th>

<%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”Default2.aspx.cs” Debug=”true” Inherits=”Default2″ %>

<th>用戶名</th>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;

<th>籍貫</th>

<html xmlns=”;
<head runat=”server”>
<title></title>
</head>
<body>
<form runat=”server”>
<div>
<asp:Repeater ID=”Repeater1″ runat=”server”
onitemcommand=”Repeater1_ItemCommand”>
<HeaderTemplate><table><tr><th>编号</th><th>姓名</th><th>状态</th><th> </th></tr></HeaderTemplate>
<ItemTemplate>
<tr><td><%# Eval %></td>
<td><%# Eval %></td>
<td><%# Convert.ToBoolean(Eval)?”启用”:”禁用”
%></td>
<td><asp:LinkButton ID=”btnSetStatus” runat=”server”
CommandArgument=’Eval’ Text='<%#
Convert.ToBoolean(Eval)?”禁用”:”启用” %>’ CommandName='<%#
Convert.ToBoolean(Eval)?”false”:”true”
%>’></asp:LinkButton></td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

<th>民族</th></tr>

</HeaderTemplate>

<ItemTemplate>

<asp:Panel ID=”plItem” runat=”server”>

<tr><td><asp:ImageButton ID=”imgInvEdit”
CommandName=”EDIT” ToolTip=”編輯”
CommandArgument='<%#Eval(“userID”)%>’

ImageUrl=”../Images/edit1.gif” runat=”server” Visible=”false” />

<asp:ImageButton ID=”imgInvDelete” CommandName=”DELETE”
ToolTip=”刪除” CommandArgument='<%#Eval(“userID”)%>’
ImageUrl=”../Images/delete.gif” runat=”server” Visible=”false”
/></td>

<td><%#Eval(“userID”)%></td>

<td><%#Eval(“username”)%></td>

<td><%#Eval(“birthplace”)%></td>

<td><%Eval(“nationality”)%></td><tr>

</asp:Panel>

<asp:Panel ID=”plItem” runat=”server”>

<tr><td><asp:ImageButton ID=”imgInvUpdate”
CommandName=”UPDATE” ToolTip=”更新”
CommandArgument='<%#Eval(“userID”)%>’

ImageUrl=”../Images/update.gif” runat=”server” Visible=”false” />

<asp:ImageButton ID=”imgInvCancel” CommandName=”CANCEL”
ToolTip=”取消” CommandArgument='<%#Eval(“userID”)%>’
ImageUrl=”../Images/cancel.gif” runat=”server” Visible=”false”
/></td>

<td><%#Eval(“userID”)%></td>

<td><%#Eval(“username”)%></td>

<td><%#Eval(“birthplace”)%></td>

<td><%Eval(“nationality”)%></td><tr>

</asp:Panel>

</ItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

C#後端代碼:

发表评论

电子邮件地址不会被公开。 必填项已用*标注