要求:
如果某项的值得长度大于某个值,就把此项的背景色进行修改;
修改或者更新某项数据;
前台代码如下:
<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(); } }
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏