zoukankan      html  css  js  c++  java
  • Repeater应用一

    要求:

    如果某项的值得长度大于某个值,就把此项的背景色进行修改;

    修改或者更新某项数据;

    前台代码如下:

     <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"
                OnItemCommand="Repeater1_ItemCommand">
                <ItemTemplate>
                    <span runat="server" id="span1">
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("CompanyName") %>'></asp:Label>
                        <asp:Button ID="Button1" runat="server" CommandName="Add" CommandArgument='<%#Eval("CustomerID") %>'
                            Text="更新" />
                        <br />
                    </span>
                </ItemTemplate>
    

    后台代码:

      protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                databind();
            }
        }
        public void databind()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
            SqlCommand cmd = new SqlCommand("SELECT  top 10 * FROM CUSTOMERS", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.Repeater1.DataSource = ds.Tables[0];
            //this.GridView1.DataKeyNames = new string[] { "CustomerID", "City" };
            this.Repeater1.DataBind();
        }
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item||  e.Item.ItemType == ListItemType .AlternatingItem)
            {
                Label lb = (Label)e.Item.FindControl("Label1");
             //   lb.Text = lb.Text.Substring(0, 4);
                if (lb.Text.Length > 4)
                {
                    HtmlGenericControl ctrl = (HtmlGenericControl)e.Item.FindControl("span1");//Span 标签在页面呈现的是HtmlGenericControl控件
                    ctrl.Attributes.Add("style","background-color:Red");
                }
            }
        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
                SqlCommand cmd = new SqlCommand("Update Customers set CompanyName=CompanyName+' Hello'" + " where CustomerID='" + e.CommandArgument.ToString() + "'", con);
                con.Open();
                cmd.ExecuteNonQuery();
                databind();
            }
        }
    

    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    poj 3461 (模式串T在主串S中出现的次数)
    hdu 1711( 模式串T在主串S中首次出现的位置)
    HDU 3980 (SG 环变成链 之前的先手变成后手)
    数据结构 Redo or Undo (模拟)
    数据结构 DNA序列 (KMP+暴力,或者STL+暴力)
    数据结构 英语词典 (STL+ set)
    数据结构 领取礼品的顺序 (STL+模拟)
    数据结构 求表达式串的后缀表达式和值 (栈+模拟)
    数据结构 下车的顺序 (STL+stack)
    数据结构 击鼓传花 (STL+模拟)
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1982579.html
Copyright © 2011-2022 走看看