我有一个带有转发器的Web用户控件,在转发器内部我有一个动态数量的单选按钮,在page_load上我试图调用转发器的onDataItemBound事件来找到radiobuttons并将oncheckedchanged事件绑定到一个方法中我的代码,但不能让它工作,这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
Repeater_Select.ItemDataBound += Repeater_Select_OnItemDataBound;
}
protected void Repeater_Select_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
customRbtn.CheckedChanged += RadioButton_Select_OnCheckedChanged;
}
}
答案 0 :(得分:0)
我设法解决了我的问题,问题是我在此OnItemDataBound
之前调用了另一个方法,它在转发器上调用DataBind()
,所以我只是颠倒了顺序并放了在另一种方法之前调用OnItemDataBound
,如下所示:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRadioOnChecked();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetFeaturePackData();
}
}
private void BindRadioOnChecked()
{
foreach (RepeaterItem item in Repeater_Select.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CustomRadioButton customRbtn = (CustomRadioButton)item.FindControl("RadioButton_Select");
customRbtn.CheckedChanged += RadioButton_Select_OnCheckedChanged;
}
}
}
现在它位于Page_Init
内,因此在Page_Load