RadioButtonList SelectedIndex 不变

发布于 2024-12-11 07:48:50 字数 3275 浏览 0 评论 0原文

我有简单的单选按钮列表,其中包含我从不更改的静态 ListItems。当用户选择单选按钮并按下提交按钮时,所选索引将返回到-1。我没有使用任何 UpdatePanels,我没有对 ViewState 进行任何更改(我尝试了两者:启用和禁用),我尝试过 AutoEventWireup true 和 false,所有值都是唯一的。没有任何帮助。

页面代码:

<div class="content">
    <div id="registration">
        <asp:Literal runat="server" ID="Literal1" Text="Registration" />
        <asp:Panel runat="server" DefaultButton="UserCreateButton">
            <table class="registration_table">
                <tr><td>
                    <asp:Literal ID="NameLiteral" runat="server" Text='<%$ Resources: Resource, LabelName %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="NameTextBox" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="SurnameLiteral" runat="server" Text='<%$ Resources: Resource, LabelSurame %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="SurnameTextBox" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="UsernameLiteral" runat="server" Text='<%$ Resources: Resource, LabelUsername %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="UsernameTextBox" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="PasswordLiteral" runat="server" Text='<%$ Resources: Resource, LabelPassword %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="PasswordTextBox" TextMode="Password" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="SexLiteral" runat="server" Text='<%$ Resources: Resource, LabelSex %>' />
                </td><td>
                    <asp:RadioButtonList ID="SexList" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
                        <asp:ListItem Text="Not chosen" Value="0" Selected="True" />
                        <asp:ListItem Text="Male" Value="1" />
                        <asp:ListItem Text="Female" Value="2" />
                    </asp:RadioButtonList>
                </td></tr>
            </table>
            <asp:Button runat="server" ID="UserCreateButton" Text='<%$ Resources: Resource, LabelRegister %>' OnClick="OnUserCreate" />
        </asp:Panel>
    </div>
</div>

页面代码隐藏:

public new UserCreatePresenter Presenter
{
    get { return base.Presenter as UserCreatePresenter; }
    set { base.Presenter = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
}

public void OnUserCreate(object sender, EventArgs e)
{
    if (Presenter != null)
    {
        Presenter.OnUserCreate(
            NameTextBox.Text,
            SurnameTextBox.Text,
            UsernameTextBox.Text,
            PasswordTextBox.Text,
            SexList.SelectedValue);
    }
}

i have simple radio button list, with static ListItems which i never change. When user selects radio and press submit button, selected index is back to -1. I am not using any UpdatePanels, i havent changed anything with ViewState (i tried both: enabled and disabled), i have tried AutoEventWireup true and false, all values are unique. Nothing helps.

Page code:

<div class="content">
    <div id="registration">
        <asp:Literal runat="server" ID="Literal1" Text="Registration" />
        <asp:Panel runat="server" DefaultButton="UserCreateButton">
            <table class="registration_table">
                <tr><td>
                    <asp:Literal ID="NameLiteral" runat="server" Text='<%$ Resources: Resource, LabelName %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="NameTextBox" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="SurnameLiteral" runat="server" Text='<%$ Resources: Resource, LabelSurame %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="SurnameTextBox" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="UsernameLiteral" runat="server" Text='<%$ Resources: Resource, LabelUsername %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="UsernameTextBox" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="PasswordLiteral" runat="server" Text='<%$ Resources: Resource, LabelPassword %>' />
                </td><td>
                    <asp:TextBox runat="server" ID="PasswordTextBox" TextMode="Password" autocomplete="off" />
                </td></tr>
                <tr><td>
                    <asp:Literal ID="SexLiteral" runat="server" Text='<%$ Resources: Resource, LabelSex %>' />
                </td><td>
                    <asp:RadioButtonList ID="SexList" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
                        <asp:ListItem Text="Not chosen" Value="0" Selected="True" />
                        <asp:ListItem Text="Male" Value="1" />
                        <asp:ListItem Text="Female" Value="2" />
                    </asp:RadioButtonList>
                </td></tr>
            </table>
            <asp:Button runat="server" ID="UserCreateButton" Text='<%$ Resources: Resource, LabelRegister %>' OnClick="OnUserCreate" />
        </asp:Panel>
    </div>
</div>

Page codebehind:

public new UserCreatePresenter Presenter
{
    get { return base.Presenter as UserCreatePresenter; }
    set { base.Presenter = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
}

public void OnUserCreate(object sender, EventArgs e)
{
    if (Presenter != null)
    {
        Presenter.OnUserCreate(
            NameTextBox.Text,
            SurnameTextBox.Text,
            UsernameTextBox.Text,
            PasswordTextBox.Text,
            SexList.SelectedValue);
    }
}

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

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

发布评论

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

评论(1

无语# 2024-12-18 07:48:50

杀死 Page_init 中的 OnClick="OnUserCreate"

设置处理程序,例如
UserCreateButton.click += new System.EventHandler(OnUserCreate);

Kill the OnClick="OnUserCreate"

set handler in Page_init like
UserCreateButton.click += new System.EventHandler(OnUserCreate);

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