mg娱乐电子4355_mg娱乐电子游戏平台
做最好的网站

mg娱乐电子4355:ASP.NET -- repeater控件的使用

时间:2019-11-22 09:11来源:计算机编程
歡迎大家來討論,修改,一定虛心接受。 转载自网络-原网址【   repeater绑定数据: 1.為什麼使用Repeater控件? protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) BindStudent(); } 關於把

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

转载自网络-原网址【

 

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

mg娱乐电子游戏平台,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#mg娱乐电子4355," 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#後端代碼:

编辑:计算机编程 本文来源:mg娱乐电子4355:ASP.NET -- repeater控件的使用

关键词: