简体   繁体   中英

Item in RadioButtonList not firing SelectedIndexChanged

I have a table cell with a RadioButtonList . When each item is selected, the SelectedIndexChanged event is supposed to fire so the app can populate a related listbox. The problem is it stopped working. Now if I selected the first entry 'Division', the event never fires. I put a break-point on the event handler and it gets called for the other entries but not for Division. I'd believe it if some other code is interfering, I just don't know where to start looking.

[update] By not working, I mean if you selected Item #2, the update works; then if you select Item #1 it doesn't. If I change where the 'Division' item appears in the list, it still has the problem. Is there something in the page load cycle that could be aborting the event handling chain?

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;
}

Its probably because the default SelectedIndex is 0. So by selecting the first radio button, the SelectedIndex does not actually change (since the first radio button would be index 0).

You can just select the first radio button programmatically by doing:

rbcohortList.SelectedIndex = 0;

after you have added the radio buttons to the list.

Is it works when you select not first item and after that you select Division item?

Try to set SelectedIndex to -1 after you add last item (ie before TableCell cellforHierarchyLevel = new TableCell(); line)

There was an exception being thrown silently from CreateChildControls(). This interrupted the call to the event handler which made it seem like the event handler wasn't being called. When I fixed the exception, the event was handled normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM