ASP错误,无法弄清楚这一点

时间:2016-11-07 02:51:57

标签: html asp.net

所以我得到了经典错误:

  

调用Sub

时不能使用括号

尝试在我的网页上执行此代码时。

以下是代码:

 <select class=<% Response.write("""" & colour & " ")%> selection" name="col" id="chosen">
          <option value="none" selected disabled>---</option>
          <option value="red">Red</option>
          <option value="blue">Blue</option>
          <option value="green">Green</option>
        </select>
      </div>
      <br>
      <input type="submit" class=<% Response.write("""" & colour & " ")%> btn" value="This one!">
    </form>
    <%
    if chosen.value = Red then PageBody.Attributes.Add("bgcolor", "red")
    elseif chosen.value = Blue then PageBody.Attributes.Add("bgcolor", "blue")
    elseif chosen.value = Green then PageBody.Attributes.Add("bgcolor", "green")
    end if
    %>

基本上我试图根据用户从HTML下拉菜单中选择的选项来更改页面的背景。我不明白我做错了什么。

1 个答案:

答案 0 :(得分:0)

aspx文件代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
        <asp:DropDownList ID="chosen1" runat="server" OnSelectedIndexChanged="chosen1_SelectedIndexChanged" AutoPostBack="true">
          <asp:ListItem Value="none" Selected="True">---</asp:ListItem>
           <asp:ListItem Value="red">Red</asp:ListItem>
           <asp:ListItem Value="blue">Blue</asp:ListItem>
           <asp:ListItem Value="green">Green</asp:ListItem>
        </asp:DropDownList>
    </form>

</body>
</html>

aspx.cs代码

Protected Sub Page_Load(sender As Object, e As EventArgs)

End Sub

Protected Sub chosen1_SelectedIndexChanged(sender As Object, e As EventArgs)
    If chosen1.SelectedIndex <> 0 Then
        Dim color = chosen1.SelectedValue.ToString()

        form1.Attributes.CssStyle.Add("background-color", color)
    End If
End Sub

它会起作用