Determine Selected Radiobutton in Constructor in C# [duplicate]
Hello,
I have this constructor:
public EmployeeCategorizationControl()
{
}
and many radio buttons:
<asp:RadioButtonList ID="selectedYesNoQuestionBlock1" runat="server" RepeatDirection="Horizontal"
OnSelectedIndexChanged="Question1GotAnswered" AutoPostBack="true">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="selectedYesNoQuestionBlock2" runat="server" RepeatDirection="Horizontal"
AutoPostBack="true" OnSelectedIndexChanged="Question2GotAnswered">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList>
In my constructor, how can I determine which radio button is selected?
Thanks in advance!
With asp.net, interacting with controls in the constructor is not a good idea because of the way the page life cycle works. You might want to glance through the page life cycle msdn page and consider Load
or Init
event instead.
You can't: the Request
isn't available until the after constructing the page instance. You have to do it at a later point in the page lifecycle.
Before Load
(during initialization for example) you can only access the selection through the request:
protected sub Page_Init(object sender, EventArgs args) {
var selection = Request.Form["selectedYesNoQuestionBlock1"];
}
Load
maps the request values to your control objects - from that point on you can access the values directly through the controls:
protected sub Page_Load(object sender, EventArgs args) {
var selection = selectedYesNoQuestionBlock1.SelectedValue;
}
精彩评论