简体   繁体   中英

ASP.NET RadioButtonList help. If I set a selectedItem, that item doesn't react to SelectedIndexChanged event

I have a RadioButtonList on my web form. I have tried two different means to set the selected item. I have tried in the markup, and in code, like the Page_Load event. It sets and displays correctly. My problem is that the selected radio button no longer responds to the SelectedIndexChanged event. The other items works as expected and if I remove the code that sets the selectedItem, then the radio button works as expected. Is there any way I can set a radio button through code, and it still behaves as I would expect. I am guessing, if u force a button to be selected, then it doesn't change. Does anyone know how to rememdey this so I can default select it, but still have it behave the way I want?

 <asp:RadioButtonList ID="rblPaymentType" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"
            RepeatLayout="Flow">
            <asp:ListItem Value="benefit" Text="Benefit" Selected="True"/>
            <asp:ListItem Value="expense" Text="Expense" />
        </asp:RadioButtonList>

This lives inside an ascx which I have an event for

  public delegate void SwitchBenefitTypeHandler(object sender, EventArgs e);
    public event SwitchBenefitTypeHandler SwitchedBenefit;

 protected void Page_Load(object sender, EventArgs e)
    {
        WireEvents();
    }


    private void WireEvents()
    {
        rblPaymentType.SelectedIndexChanged += (sender, args) => SwitchedBenefit(sender, args);

    }

Then on the aspx, I wire a handler function to that event.

if (header is PaymentHeader)
                (header as PaymentHeader).SwitchedBenefit += (paymentForm as PaymentBaseControl).Update;

Finally the handler function

public override void Update(object sender, EventArgs e)
    {
        if (sender is RadioButtonList)
        {
            IsExpense = (sender as RadioButtonList).SelectedValue == "expense";
            UpdateCalcFlag();
            UpdateDropDownDataSources();
            UpdatePaymentTypeDropDown();
            ResetBenefitLabels();
            FormatAmountTextBox();
        }
    }

I hope that's enough code. Everything works great when I don't set the SelectedItem in the RadioButtonList but I need it set.

Here's a link to someone with the same problem. It is ASP.NET AJAX related. Click Here Thanks, ~ck in San Diego

尝试这个:

radiobutton.Items.FindByText("VALUE").Selected = true;

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