ASP.NET上的radiobuttons出错

时间:2011-03-03 22:20:36

标签: c# asp.net

为什么下面的代码显示错误?请更正:

    protected void Button1_Click(object sender, EventArgs e)
{
    if (RadioButtonList1.SelectedIndex='0') 
    {
        Response.Redirect("page1.aspx");
    }
    else if (RadioButtonList1.SelectedIndex='1') 
    {
        Response.Redirect("page2.aspx");
    }
    else if (RadioButtonList1.SelectedIndex='2') 
    {
        Response.Redirect("page3.aspx");
    }
}

我的设计代码是:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem>item1</asp:ListItem>
            <asp:ListItem>item2</asp:ListItem>
            <asp:ListItem>item3</asp:ListItem>
        </asp:RadioButtonList></div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:6)

因为SelectedIndex是一个整数,而不是一个字符。摆脱单引号。 =是一个赋值运算符,而不是比较。

将其更改为

if (RadioButtonList1.SelectedIndex == 0) 
{
    Response.Redirect("page1.aspx");
}
else if (RadioButtonList1.SelectedIndex == 1) 
{
    Response.Redirect("page2.aspx");
}
else if (RadioButtonList1.SelectedIndex == 2) 
{
    Response.Redirect("page3.aspx");
}

答案 1 :(得分:6)

SelectedIndexint,不是字符串:

if (RadioButtonList1.SelectedIndex == 0) 
{
    Response.Redirect("page1.aspx");
}
else if (RadioButtonList1.SelectedIndex == 1) 
{
    // and so on
}