在 C# 的构造函数中确定选定的单选按钮

发布于 2024-11-16 18:09:07 字数 975 浏览 4 评论 0原文

您好,

我有这个构造函数:

public EmployeeCategorizationControl()
        {


        }

和许多单选按钮:

<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>

在我的构造函数中,如何确定选择了哪个单选按钮?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-11-23 18:09:07

对于 asp.net,与构造函数中的控件交互并不是一个好主意,因为

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.

二手情话 2024-11-23 18:09:07

您不能:请求< /code>在构建页面实例之后之后才可用。您必须稍后在页面生命周期

Load之前(例如在初始化期间),您只能通过请求访问选择:

protected sub Page_Init(object sender, EventArgs args) {
    var selection = Request.Form["selectedYesNoQuestionBlock1"];
}

Load将请求值映射到您的控制对象 - 从那时起您可以访问直接通过控件获取值:

protected sub Page_Load(object sender, EventArgs args) {
    var selection = selectedYesNoQuestionBlock1.SelectedValue;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文