ASP.NET中repeater控制項用法執行個體_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了ASP.NET中repeater控制項用法。分享給大家供大家參考。具體實現方法如下:

repeater綁定資料:

複製代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
        if(!IsPostBack)
            BindStudent();
}

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

刪除資料:

複製代碼 代碼如下:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
        if (e.CommandName == "Delete")
        {
            string str = ConfigurationManager.ConnectionStrings["stucnn"].ConnectionString;
            using (SqlConnection sqlCnn = new SqlConnection(str))
            {
                using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
                {
                    sqlCnn.Open();
                    sqlCmm.CommandText = "delete from student where sid="
                        + e.CommandArgument.ToString();
                    sqlCmm.ExecuteNonQuery();
                }
            }
            this.BindStudent();
        }
        else if (e.CommandName == "Edit")
        {
            Server.Transfer("Edit.aspx?sid=" + e.CommandArgument.ToString());
        }
}

前台:

複製代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <%--<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
    <hr />
    </HeaderTemplate>
    <ItemTemplate><div>
    <asp:Label ID="lblSid" runat="server" Text='<%# Eval("sid") %>'></asp:Label>
    <asp:Label ID="lblSname" runat='server' Text='<%# Eval("sname") %>'></asp:Label>
    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# Eval("photo") %>' /></div>
    </ItemTemplate>
    <SeparatorTemplate>
    <hr />
    </SeparatorTemplate>
    <AlternatingItemTemplate>
    <div style="
    <asp:Label ID="lblSid" runat="server" Text='<%# Eval("sid") %>'></asp:Label>
    <asp:Label ID="lblSname" runat='server' Text='<%# Eval("sname") %>'></asp:Label>
    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# Eval("photo") %>' /></div>
    </AlternatingItemTemplate>
    <FooterTemplate><hr /></FooterTemplate>
    </asp:Repeater>--%>
    <div>

        <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("sid") %></td><td><%# Eval("sname") %></td>
        <td><img width="60px" height="60px" src='<%# "images/" + Eval("photo") %>' /></td>
        <td><asp:LinkButton ID="btnDelete" runat="server" Text="刪除" CommandName='Delete' CommandArgument='<%# Eval("sid") %>'></asp:LinkButton></td>
        <td><asp:LinkButton ID="btnEdit" runat="server" Text="編輯" CommandName='Edit' CommandArgument='<%# Eval("sid") %>'></asp:LinkButton></td>
        </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
        <tr style="
        <td><%# Eval("sid") %></td><td><%# Eval("sname") %></td>
        <td><img width="60px" height="60px" src='<%# "images/" + Eval("photo") %>' /></td>
        <td><asp:LinkButton ID="btnDelete" runat="server" Text="刪除" CommandName='Delete' CommandArgument='<%# Eval("sid") %>'></asp:LinkButton></td>
        <td><asp:LinkButton ID="btnEdit" runat="server" Text="編輯" CommandName='Edit' CommandArgument='<%# Eval("sid") %>'></asp:LinkButton></td>
        </tr>
        </AlternatingItemTemplate>
        <SeparatorTemplate>
        <tr><td colspan="5"><hr /></td></tr>
        </SeparatorTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>

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

啟用,禁用:

複製代碼 代碼如下:
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(str))
            {
                using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
                {
                    sqlCnn.Open();
                    sqlCmm.CommandText = "update student set status=@status where sid="
                        + e.CommandArgument.ToString();
                    sqlCmm.Parameters.AddWithValue("@status",e.CommandName);
                    sqlCmm.ExecuteNonQuery();
                }
            }
            this.BindStudent();
        }
}

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" 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("SID") %></td>
        <td><%# Eval("sname") %></td>
        <td><%# Convert.ToBoolean(Eval("status"))?"啟用":"禁用" %></td>
        <td><asp:LinkButton ID="btnSetStatus" runat="server" CommandArgument='Eval("sid")' Text='<%# Convert.ToBoolean(Eval("status"))?"禁用":"啟用" %>' CommandName='<%# Convert.ToBoolean(Eval("status"))?"false":"true" %>'></asp:LinkButton></td></tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

希望本文所述對大家的asp.net程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.