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

Posted by Hcabnettek on Stack Overflow See other posts from Stack Overflow or by Hcabnettek
Published on 2009-09-18T19:56:12Z Indexed on 2010/05/30 1:02 UTC
Read the original article Hit count: 311

Hi Guys, 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

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET