【问题标题】:Input string not in correct format (Radiobuttonlist) asp.net输入字符串格式不正确(单选按钮列表)asp.net
【发布时间】:2017-01-27 01:21:09
【问题描述】:

我正在尝试从单选按钮中检索文本以在我的 sql 查询中使用。 我输入了这个:

 int cardtype = int.Parse(rbcard.SelectedItem.Text);

然后发生错误

 Input string not in correct format

似乎是什么问题?

【问题讨论】:

标签: asp.net radiobuttonlist


【解决方案1】:

让我们说单选按钮列表看起来像:

 <asp:RadioButtonList ID="rdID" runat="server">

<asp:ListItem Text ="Item1" Value="1" />
<asp:ListItem Text ="Item2" Value="2" />
<asp:ListItem Text ="Item3" Value="3" />
<asp:ListItem Text ="Item4" Value="4" />

</asp:RadioButtonList>

然后获取选定的值:

string selectedValue = rdID.SelectedValue;
        Response.Write(selectedValue);

然后将值作为字符串获取后,您可以将其解析为int,如:

int x = Int32.Parse(selectedValue );

希望对你有帮助

【讨论】:

  • 很高兴为您提供帮助:)
【解决方案2】:

看起来您实际上是在尝试解析单选按钮列表项的文本而不是值。

您的代码对我来说是正确的,我只是确保您在尝试解析 int 之前检查是否选择了一个项目。

HTML

<asp:RadioButtonList ID="rbcard" runat="server">

<asp:ListItem Text ="1" Value="1" />
<asp:ListItem Text ="2" Value="2" />
<asp:ListItem Text ="3" Value="3" />
<asp:ListItem Text ="4" Value="4" />

</asp:RadioButtonList>

<asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="select value" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

代码背后

protected void Button1_Click(object sender, EventArgs e)
    {
        if (rbcard.SelectedItem != null)
        {
            int cardtype = int.Parse(rbcard.SelectedItem.Text);
            Label1.Text = "You Selected: " + cardtype;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多