以编程方式设置转发器内面板/ div的背景

时间:2014-09-17 14:52:21

标签: c# repeater

我有这样的转发器:                    

        <asp:Panel runat="server"  ID="panel1" style="width: 105mm; height: 73mm;>
            <div class="tiles" style="float: right; width: 100px"></div>
            <div class="tiles" style="float: right; width: 100%;text-align:center;padding-top:20px;">
                <%# DataBinder.Eval(Container.DataItem, "image") %>
            </div>
            <div class="tiles" style="padding: 0 20px 0 0;width:93%;text-align:center">
                <%# DataBinder.Eval(Container.DataItem, "Name") %><br />
            </div>
            <div class="tiles" style="padding: 0 20px 0 0;text-align:center">
                <img src="../pics/sallogo.png" />
            </div>
            <div class="tiles" style="padding: 0 20px 0 0;text-align:center">
                  <%# DataBinder.Eval(Container.DataItem, "Family") %><br />
            </div>
        </asp:Panel>
      </ItemTemplate>

    </asp:Repeater>

我需要在代码后面设置面板背景, 我,使用这个C#代码用于转发器,如果​​j == 0,则将背景设置为特定图像,否则设置为另一个图像:

       protected void Page_Load(object sender, EventArgs e)
       {


        SqlConnection con = new SqlConnection(helper.conn);
        SqlCommand com1 = new SqlCommand("select id,name,guest  from Person where Id='1', con);
        DataSet ds1 = new DataSet();
        SqlDataAdapter da1 = new SqlDataAdapter(com1);
        da1.Fill(ds1);
        con.Open();
        com1.ExecuteNonQuery();
        //gdvusers.DataSource = ds1;
        //gdvusers.DataBind();
        con.Close();

        rpt_print.DataSource = ToList(ds1.Tables[0]);
        rpt_print.DataBind();
    }

    private List<Person> ToList(DataTable table)
    {
        var categoryList = new List<Person>(table.Rows.Count);
        foreach (DataRow row in table.Rows)
        {


                var values = row.ItemArray;
                String i = values[2].ToString();
                int end = Int32.Parse(i);
                for (int j = 0; j< end+1; j++)
                {

                var category = new Person()
                {
                    Id = long.Parse(values[0].ToString()),// values[0],
                    Name = j == 0 ? "نیکوکار محترم "+values[1].ToString() :"مهمان نیکوکار محترم "+ values[1].ToString(),
                  //  Mobile = values[4].ToString(),
                   // UserId = int.Parse(values[6] != null ? values[6].ToString() : "0"),// values[0],
                    image = j==0?createbarcode(values[0].ToString()):createbarcode(9999+values[0].ToString()),
                    Family = j == 0 ? "تعداد مهمان شما : " + values[2].ToString() : " ",
                };
                categoryList.Add(category);
            }
        }
            return categoryList.ToList();

    }

以及之前使用的person类的代码:

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }

    public string Meli { get; set; }
    public string Mobile { get; set; }
    public Nullable<DateTime> regdate { get; set; }
    public Nullable<int> UserId { get; set; }
    public string image { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您可以使用ItemDataBound事件来执行此操作。

WebForm(aspx)

<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:Panel ID="Panel1" runat="server">
            // Your other code here
        </asp:Panel>
    </ItemTemplate>
</asp:Repeater>

Code Behind(cs):

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var panel = (Panel) e.Item.FindControl("Panel1");

        if (true /*Condition for setting panel background color*/)
        {
            panel.BackgroundColor = Color.Red;
        }
    }
}