ASP.NET上的radiobuttons出错

loc*_*ock -1 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的设计代码是:

<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>
Run Code Online (Sandbox Code Playgroud)

Bra*_*don 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");
}
Run Code Online (Sandbox Code Playgroud)


Fre*_*örk 6

SelectedIndex是一个int,而不是一个字符串:

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