首页 > 代码库 > repeater中后台动态为控件添加属性

repeater中后台动态为控件添加属性

在此贴出repeater中的ItemDataBound事件中的代码:

private void ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//**对每一行操作,两种枚举必须都要进行判断
            {
                TextBox tb = e.Item.FindControl("rate") as TextBox;
                Button btnEdit = e.Item.FindControl("BtnEdit") as Button;
                Button btnUpd = e.Item.FindControl("BtnUpd") as Button;
                Label lbl_ID = e.Item.FindControl("lbl_ID") as Label;
                LinkButton lb_Edit = e.Item.FindControl("lb_Edit") as LinkButton;

                if (tb != null && btnEdit != null && btnUpd != null && lbl_ID != null)
                {
                 
                    tb.ID = "rate_" + lbl_ID.Text;
                    btnEdit.ID = "BtnEdit_" + lbl_ID.Text;
                    btnUpd.ID = "BtnUpd_" + lbl_ID.Text;
                    btnEdit.Attributes.Add("onclick", "edit(" + lbl_ID.Text + ")");//为该控件添加onclick事件,js为edit(id);
                    btnUpd.Attributes.Add("onclick", "update(" + lbl_ID.Text + ")");//同上
} } }