如果CheckBoxList的ListItems
與aspnet一起添加,無論是在代碼後面還是在.aspx頁面中,ViewState都將確保它將看到已禁用的複選框,即使它們沒有發送到服務器。
protected void Button1_Click(object sender, EventArgs e)
{
List<string> selectedValues = new List<string>();
Label1.Text = "";
foreach (ListItem item in myCheckBoxList.Items)
{
if (item.Selected)
{
selectedValues.Add(item.Value);
Label1.Text += item.Value + "<br>";
}
}
}
而且使完成這個例子:
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br /><br />
<asp:CheckBoxList ID="myCheckBoxList" runat="server">
<asp:ListItem Text="Blueberry" Value="Blueberry"></asp:ListItem>
<asp:ListItem Text="Raspberry" Value="Raspberry"></asp:ListItem>
<asp:ListItem Text="Blackberry" Value="Blackberry"></asp:ListItem>
<asp:ListItem Text="Strawberry" Value="Strawberry" Enabled="false" Selected="True"></asp:ListItem>
<asp:ListItem Text="Gooseberry" Value="Gooseberry" Enabled="false" Selected="True"></asp:ListItem>
</asp:CheckBoxList>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
我想這是因爲禁用複選框並沒有真正發送到服務器POST。看到[這個答案](http://stackoverflow.com/questions/4727974/how-to-post-submit-an-input-checkbox-that-is-disabled) –