主頁(yè) > 知識(shí)庫(kù) > ASP.NET中repeater控件用法實(shí)例

ASP.NET中repeater控件用法實(shí)例

熱門標(biāo)簽:寧夏機(jī)器人電銷 外呼電銷機(jī)器人軟件 威海電銷 400免費(fèi)電話怎么辦理 河南語(yǔ)音外呼系統(tǒng)公司 關(guān)于宗地圖標(biāo)注技術(shù)規(guī)范 400電話辦理最優(yōu)質(zhì) t3出行地圖標(biāo)注怎么做 河北網(wǎng)絡(luò)回?fù)芡夂粝到y(tǒng)

本文實(shí)例講述了ASP.NET中repeater控件用法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

repeater綁定數(shù)據(jù):

復(fù)制代碼 代碼如下:
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();
            }
        }
}

刪除數(shù)據(jù):

復(fù)制代碼 代碼如下:
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());
        }
}

前臺(tái):

復(fù)制代碼 代碼如下:
%@ 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">編號(hào)/td>td style="width:100px">姓名/td>
        td style="width:100px">圖片/td>td>nbsp;/td>td>nbsp;/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>

啟用,禁用:

復(fù)制代碼 代碼如下:
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>編號(hào)/th>th>姓名/th>th>狀態(tài)/th>th>nbsp;/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>

希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • asp.net使用Repeater控件中的全選進(jìn)行批量操作實(shí)例
  • 在Repeater控件中通過(guò)Eval的方式綁定Style樣式代碼
  • Repeater控件與PagedDataSource結(jié)合實(shí)現(xiàn)分頁(yè)功能
  • Repeater控件實(shí)現(xiàn)編輯、更新、刪除等操作示例代碼
  • 給Repeater控件里添加序號(hào)的5種才常見(jiàn)方法介紹
  • Repeater控件綁定的三種方式
  • Repeater控件動(dòng)態(tài)變更列(Header,Item和Foot)信息實(shí)現(xiàn)思路
  • 如何取得Repeater控件選擇的項(xiàng)目及注意事項(xiàng)
  • Repeater控件分別綁定數(shù)組和ArrayList實(shí)現(xiàn)思路
  • Repeater控件數(shù)據(jù)導(dǎo)出Excel(附演示動(dòng)畫)
  • 淺析Repeater控件的使用 (原樣導(dǎo)出和動(dòng)態(tài)顯示/隱藏Repeater中的列)

標(biāo)簽:池州 咸寧 樂(lè)山 吉林 淮北 賀州 廣元 固原

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET中repeater控件用法實(shí)例》,本文關(guān)鍵詞  ASP.NET,中,repeater,控件,用法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET中repeater控件用法實(shí)例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET中repeater控件用法實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章