[英]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.