简体   繁体   English

ASP.NET RadioButtonList帮助。 如果设置了selectedItem,则该项目不会对SelectedIndexChanged事件作出反应

[英]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. 我的网络表单上有一个RadioButtonList。 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. 我已经在标记和代码中尝试过,例如Page_Load事件。 It sets and displays correctly. 它会正确设置并显示。 My problem is that the selected radio button no longer responds to the SelectedIndexChanged event. 我的问题是,选定的单选按钮不再响应SelectedIndexChanged事件。 The other items works as expected and if I remove the code that sets the selectedItem, then the radio button works as expected. 其他项目按预期工作,如果我删除设置selectedItem的代码,则单选按钮按预期工作。 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 这生活在我有一个事件的ascx中

  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. 然后在aspx上,将处理程序函数连接到该事件。

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. 当我不在RadioButtonList中设置SelectedItem时,一切都很好,但是我需要设置它。

Here's a link to someone with the same problem. 这是指向有相同问题的人的链接。 It is ASP.NET AJAX related. 它与ASP.NET AJAX有关。 Click Here Thanks, ~ck in San Diego 单击此处谢谢,〜ck在圣地亚哥

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net RadioButtonList SelectedIndexChanged事件未触发 - ASP.Net RadioButtonList SelectedIndexChanged event not firing 在 asp.net C# 中第一次执行该事件后,如何让 RadioButtonList_SelectedIndexChanged 事件处理程序执行? - How do I get the RadioButtonList_SelectedIndexChanged event handler to execute after the first execution of that event in asp.net C#? 当RadioButtonList选择asp.net C#时,SelectedIndexChanged无法正常工作? autopostback还设置为true吗? - SelectedIndexChanged not working while RadioButtonList selected asp.net c#? autopostback also set to true? 未调用 ASP.NET 组合 selectedIndexChanged 事件 - ASP.NET combo selectedIndexChanged event is not called RadioButtonList中的项目未触发SelectedIndexChanged - Item in RadioButtonList not firing SelectedIndexChanged RadioButtonList SelectedIndexChanged事件未触发 - RadioButtonList SelectedIndexChanged event not firing C#-Gridview帮助。 (ASP.NET) - C# - Gridview Help. (ASP.NET) 错误错误:尝试从代码隐藏中设置asp.net单选按钮列表所选项目返回错误 - Bug error: trying to set asp.net radiobuttonlist selected item from codebehind return an error 使用ASP.Net ListView.ExtractItemValues,如何在ListViewDataItem的RadioButtonList中获得所选项目? - Using ASP.Net ListView.ExtractItemValues, how do I get the selected item in a RadioButtonList in the ListViewDataItem? 带C#RadiobuttonList的ASP.net不会触发回发 - ASP.net w/ C# RadiobuttonList doesn't trigger a postback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM