• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    ASP.NET中repeater控件用法实例

    本文实例讲述了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>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>

    启用,禁用:

    复制代码 代码如下:
    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>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>

    希望本文所述对大家的asp.net程序设计有所帮助。

    您可能感兴趣的文章:
    • asp.net使用Repeater控件中的全选进行批量操作实例
    • 在Repeater控件中通过Eval的方式绑定Style样式代码
    • Repeater控件与PagedDataSource结合实现分页功能
    • Repeater控件实现编辑、更新、删除等操作示例代码
    • 给Repeater控件里添加序号的5种才常见方法介绍
    • Repeater控件绑定的三种方式
    • Repeater控件动态变更列(Header,Item和Foot)信息实现思路
    • 如何取得Repeater控件选择的项目及注意事项
    • Repeater控件分别绑定数组和ArrayList实现思路
    • Repeater控件数据导出Excel(附演示动画)
    • 浅析Repeater控件的使用 (原样导出和动态显示/隐藏Repeater中的列)
    上一篇:.Net遍历窗体上控件的方法
    下一篇:asp.net 错误:0x8007000B 异常的解决方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    ASP.NET中repeater控件用法实例 ASP.NET,中,repeater,控件,用法,