C#控件之Repeater控件使用【彩民之家论坛9066777】

2019-10-12 20:20 来源:未知

 

repeater绑定数据:

newRow["photo"] = item.FindControl("txtPhoto")).Text;

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

删除数据:

newRow["IsHave"] = item.FindControl("lblsound")).Text;

{

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

//添加一行

<td><%Eval("nationality")%></td><tr>

转载自网络-原网址【

DataRow newRow = dt.NewRow();

}

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

dt.Rows.Add;

//數據綁定

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();
}
}

//首先,恢复数据源

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

启用,禁用:

<table >
<asp:Repeater ID="rp_List" runat="server" onitemcommand="rp_List_ItemCommand">
<HeaderTemplate>
<thead>
<tr>
<th>
ID
</th>
<th>
中文内容
</th>
<th>
英文内容
</th>
<th>
是否上传声音<%--(添加时<span >必填</span>)--%>
</th>
<th width="80px">
头像
</th>
<th width="80px">
对话排序
</th>
<th width="80px">
最大时间
</th>
<th width="80px">
声音时间
</th>
<th>
操作
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Container.ItemIndex 1 %>
<asp:HiddenField ID="hidid" runat="server" Value='<%#Eval %>' />
</td>

<table>

<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>

dt.Rows.Add;

<th>用戶名</th>

<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>

row["maxsecond"] = "1";

protected void reportRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)

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();
}
}
}

DataRow row = dt.NewRow();

if(Page.IsPostBack)

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

/// <summary>
/// 根据repeater相对应的列名,定义数据源datatable的schema
/// </summary>
/// <param name="columns">列名</param>
/// <returns></returns>
public DataTable DefineDataTableSchema(string columns)
{
DataTable dt = new DataTable();
string[] columnsAry = columns.Split;
foreach (string str in columnsAry)
{
dt.Columns.Add;
}
return dt;
}

BindData();

<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>

row["photo"] = "";

break;

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" %>

<td>
<asp:TextBox ID="txtCountentzh" runat="server" Text='<%#Eval("Contentzh") %>' ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtCountenten" runat="server" Text='<%#Eval("Contenten") %>' ></asp:TextBox>
</td>
<td>
<%--<asp:FileUpload ID="FileUpload" runat="server" CssClass="form-control" />--%>
<asp:Label ID="lblsound" runat="server" Text='<%#Eval %>'></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPhoto" runat="server" Text='<%#Eval %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtOrder" runat="server" Text='<%#Eval("orderby") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtMaxsecond" runat="server" Text='<%#Eval("maxsecond") %>'></asp:TextBox>

<ItemTemplate>

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

row["Contenten"] = "";

}

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

}

break;

</td>
<td>
<asp:TextBox ID="txtSoundSize" runat="server" Text='<%#Eval("soundsize") %>'></asp:TextBox>
</td>
<td>
<%--<a href="DuihuaDelete.aspx?id=<%#Eval %>&page=<%=CurrentPage %>&pianzhangid=<%=pzid %>" onclick="return confirm('是否删除该数据')">删除</a>--%>
<%--<a href="DuihuaEdit.aspx?id=<%#Eval %>&pianzhangid=<%=pzid %>" >编辑</a>
<a href="DuihuaiDetail.aspx?id=<%#Eval %>" title="详情">详情</a>--%>
<asp:LinkButton ID="lbtnDelete" runat="server" CssClass="btn btn-danger" OnClientClick='return confirm' CommandName="del" CommandArgument='<%#Eval %>'>删除</asp:LinkButton>
<asp:LinkButton ID="lbtnEdit" runat="server" CssClass="btn btn-default" CommandName="edit" CommandArgument='<%#Eval %>'>编辑</asp:LinkButton>
</td>
</ItemTemplate>
<FooterTemplate>
<tr runat="server" visible="<%#rp_List.Items.Count==0 %>">
<td colspan="9" >
未找到符合条件的信息
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
</table>

<td><%#Eval("username")%></td>

后台代码:

//更新數據庫

rp_List.DataBind();

protected void BindData()

protected void btnAddNewRow_Click(object sender, EventArgs e)
{

id="";

newRow["maxsecond"] = item.FindControl("txtMaxsecond")).Text;

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

{

<td><%#Eval("birthplace")%></td>

newRow["Contenten"] = item.FindControl("txtCountenten")).Text;

1.為什麼使用Repeater控件?

foreach (RepeaterItem item in rp_List.Items)

<td><%#Eval("userID")%></td>

}

 

newRow["Contentzh"] = item.FindControl("txtCountentzh")).Text;

</HeaderTemplate>

row["soundsize"] = "1";

<td><%#Eval("birthplace")%></td>

row["Contentzh"] = "";

//從數據庫刪除

row["IsHave"] = "未上传";

}

newRow["id"] = ((HiddenField)item.FindControl.Value;

2.Repeater控件之好處

rp_List.DataSource = dt;

  例:以下例子展示了如何對Repeater控件進行綁定,并實現在table中實現編輯和更新數據行的操作

DataTable dt = DefineDataTableSchema(hfRptColumns.Value);

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

row["orderby"] = "0";

<td><%#Eval("userID")%></td>

newRow["orderby"] = item.FindControl("txtOrder")).Text;

C#後端代碼:

newRow["soundsize"] = item.FindControl("txtSoundSize")).Text;

}

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

case "DELETE":

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

{

<th>籍貫</th>

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

break;

  我們可以自由的定義Table的表頭、表身和表尾。

</asp:Panel>

reportRepeater.DataSource = dt;

3.Repeater控件一些基礎設定

}

}

<td><%#Eval("username")%></td>

</table>

<tr><th></th>

<td><%Eval("nationality")%></td><tr>

  關於把從數據庫讀取的數據綁定到前台頁面,我們可以使用DataGrid、DataGridView以及Repeater來佈局,三者均可以直接綁定DataTable中的字段,但是對於特殊的表格,如需要跨行或跨列顯示的數據,則需要重畫一個table,然後再綁定數據。

{

}

protected void reportRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)//Item綁定時可以在此做一些操作

string sql = "select * from user";

BindData();

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

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

{

case "CANCEL":

<th>用戶ID</th>

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

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

}

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

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

protected void Page_Load(object sender, EventArgs e)
{

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;
}

<FooterTemplate>

{

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

</asp:Panel>

html端代碼:

</FooterTemplate>

reportRepeater.DataBind();

}

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

{

{

</ItemTemplate>

版权声明:本文由彩民之家高手论坛发布于编程技术,转载请注明出处:C#控件之Repeater控件使用【彩民之家论坛9066777】