repeater练习
namespace repeater
{
public partial class itemdatabound : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
var row=drv.Row;
string name= (string)row.ItemArray[2];
if (name == "1")
{
HtmlGenericControl span=(HtmlGenericControl)e.Item.FindControl("span1");//span行变成红色
span.Style["background-color"] = "red";
//TextBox text=(TextBox)e.Item.FindControl("TextBox1");//文本框背景变成红色
//text.BackColor = Color.Red;
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "add")
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("update images set name=name+1 where id=@id", conn);
int id = Convert.ToInt32(e.CommandArgument);
SqlParameter para = new SqlParameter("@id", id);
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
}
Repeater1.DataBind();//强制刷新数据
}
}
}
}