RadioButtonList SelectedIndex不变

时间:2011-10-21 10:12:59

标签: asp.net postback radiobuttonlist

我有简单的单选按钮列表,带有我永远不会改变的静态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);
    }
}

1 个答案:

答案 0 :(得分:0)

杀死OnClick="OnUserCreate"

在Page_init中设置处理程序 UserCreateButton.click += new System.EventHandler(OnUserCreate);