RadioButtonList中的项目未触发SelectedIndexChanged

时间:2010-08-10 18:51:34

标签: c# asp.net event-handling

我有一个带有RadioButtonList的表格单元格。选择每个项目后,SelectedIndexChanged事件应该触发,以便应用程序可以填充相关的列表框。问题是它停止了工作。现在,如果我选择第一个条目“分部”,则事件永远不会触发。我在事件处理程序上设置了一个断点,并为其他条目调用它,但不为分区调用。我相信如果其他一些代码干扰,我只是不知道从哪里开始寻找。

[更新] 如果不工作,我的意思是如果您选择了第2项,则更新有效;那么如果你选择第1项,它就不会。如果我更改“分部”项目出现在列表中的位置,则仍然存在问题。页面加载周期中是否存在可能导致事件处理链中止的内容?

private TableCell foo()    
{
hierarchyLevel = new RadioButtonList();

ListItem DivisionItem = new ListItem();
DivisionItem.Text = "Division";
DivisionItem.Value = "afe_dvsn";        
hierarchyLevel.Items.Add(DivisionItem);

ListItem DistrictItem = new ListItem();
DistrictItem.Text = "District";
DistrictItem.Value = "afe_dist";
hierarchyLevel.Items.Add(DistrictItem);

ListItem AreaItem = new ListItem();
AreaItem.Text = "Area";
AreaItem.Value = "afe_supt";
hierarchyLevel.Items.Add(AreaItem);

ListItem ForemanItem = new ListItem();
ForemanItem.Text = "Foreman";
ForemanItem.Value = "afe_frmn";
hierarchyLevel.Items.Add(ForemanItem);

ListItem AfeCodeItem = new ListItem();
AfeCodeItem.Text = "AFE Code";
AfeCodeItem.Value = "afe_code";
hierarchyLevel.Items.Add(AfeCodeItem);

ListItem PropertyItem = new ListItem();
PropertyItem.Text = "Property";
PropertyItem.Value = "prop_sub";
hierarchyLevel.Items.Add(PropertyItem);

TableCell cellforHierarchyLevel = new TableCell();
cellforHierarchyLevel.ID = "hierarchyLevel";
cellforHierarchyLevel.Controls.Add(hierarchyLevel);

hierarchyLevel.EnableViewState = true;

hierarchyLevel.AutoPostBack = true;

hierarchyLevel.SelectedIndexChanged += new EventHandler(hierarchyLevel_SelectedIndexChanged);

return cellforHierarchyLevel;
}

3 个答案:

答案 0 :(得分:0)

可能是因为默认SelectedIndex为0.因此,通过选择第一个单选按钮,SelectedIndex实际上不会改变(因为第一个单选按钮将是索引0)。

您可以通过以下方式以编程方式选择第一个单选按钮:

rbcohortList.SelectedIndex = 0;
将单选按钮添加到列表后

答案 1 :(得分:0)

当您选择不是第一个项目时,它是否有效,之后您选择Division项目?

在添加最后一项(即SelectedIndex行之前)后,尝试将-1设置为TableCell cellforHierarchyLevel = new TableCell();

答案 2 :(得分:0)

从CreateChildControls()静默抛出异常。这中断了对事件处理程序的调用,使得它看起来好像没有调用事件处理程序。当我修复异常时,事件处理正常。

相关问题