工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

要求:

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

修改或者更新某项数据;

前台代码如下:

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

posted on 2011-03-13 09:59  梦里故乡  阅读(381)  评论(0编辑  收藏  举报