开发者

Determine Selected Radiobutton in Constructor in C# [duplicate]

This question already has answers here: How to get the selected value from RadioButton开发者_StackOverflow中文版List? (5 answers) Closed 10 years ago.

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜