【问题标题】:How can I use radiobuttonlist selectedvalue in autopostback event?如何在自动回发事件中使用 radiobuttonlist selectedvalue?
【发布时间】:2015-03-19 20:33:03
【问题描述】:

html 文件:

<p>Delivery Type :</p>
  <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
  <asp:ListItem Value="0">Electronic</asp:ListItem>
  <asp:ListItem Value="1">Paper Mail</asp:ListItem>
</asp:RadioButtonList>

代码隐藏页面:

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(RadioButtonList1.SelectedItem.Value = )
    {

    }
}

我想使用单选按钮,以便如果用户选择电子邮件选项,则应显示电子邮件字段。因此,据我所知,我正在尝试如果 radiobuttonlist 选择的值为 0 则应显示电子邮件字段。但我在if 条件本身中遇到错误:

无法将类型字符串转换为布尔型

【问题讨论】:

  • 无法将类型字符串转换为布尔型
  • RadioButtonList1.SelectedItem.Value 是字符串,所以在 if 中您需要将其与字符串进行比较,例如 RadioButtonList1.SelectedItem.Value == "0"

标签: c# asp.net radiobuttonlist


【解决方案1】:

当使用多个单选按钮(而不是单选按钮列表)并手动将它们设置为同一组时:

     <asp:RadioButton ID="RadioButton1" runat="server" GroupName="RadioButtonList" />
    <br />
    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="RadioButtonList" />

以下应该可以工作,

if(RadioButton1.Checked)
{

}

这种方式应该不需要数据类型转换,避免您似乎遇到的错误。希望这行得通。

【讨论】:

    【解决方案2】:

    试试

    if(RadioButtonList1.SelectedItem.Value == "0"){
    }
    

    【讨论】:

    • == 不能应用于 string 和 int 类型的操作数
    • @SachinR.Patel 试试RadioButtonList1.SelectedItem.Value == "0"
    猜你喜欢
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多