澳门至尊网站-首页

您的位置:澳门至尊网站 > 免费资源 > 控件之Repeater控件使用,repeater控件的选拔

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

2019-10-18 13:42

歡迎大家來討論,修改,一定虛心接受。

转发自网络-原网站【

 

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#後端代碼:

protected void Page_Load(object sender, EventArgs e)
{

if(Page.IsPostBack)

{

BindData();

}

}

 

protected void BindData()

{

string sql = "select * from user";

DAL.DAL dal = new DAL.DAL();

DataTable dt = dal.ExecuteDataSet(Properties.Settings.Default.DBWF, sql).Tables[0];

reportRepeater.DataSource = dt;

reportRepeater.DataBind();

}

protected void reportRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)

{

switch (e.CommandName.Trim().ToUpper())
{
case "EDIT":
{
id = e.CommandArgument.ToString();
break;
}
case "UPDATE":

{

//更新數據庫

break;

}

case "CANCEL":

{

id="";

break;

}

case "DELETE":

{

//從數據庫刪除

break;

}

//數據綁定

BindData();

}

protected void reportRepeater_ItemDataBound(object sender, RepeaterItem伊夫ntArgs e)//Item綁定時可以在这里做一些操作

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//
{

System.Data.DataRowView record = (System.Data.DataRowView)e.Item.DataItem;
string userID= record["userID"].ToString();
if (userID.Equals(id))//當點擊差异的按鈕時對Pannel控件進行隱藏和顯示,達到在table內編輯行的意义
{
((Panel)e.Item.FindControl("plItem")).Visible = false;
((Panel)e.Item.FindControl("plEdit")).Visible = true;
}
else
{
((Panel)e.Item.FindControl("plItem")).Visible = true;
((Panel)e.Item.FindControl("plEdit")).Visible = false;
}

}

}

本文由澳门至尊网站发布于免费资源,转载请注明出处:控件之Repeater控件使用,repeater控件的选拔

关键词: