我有一个应用程序,它位于 .Net1.1 中,并使用 Visual Studio 2008 升级到 3.5。
有一个页面有一个复选框和一个使用 Javascript 启用和禁用的列表框,如果启用,我们可以选择列表中的项目,然后单击按钮,将处理所选项目。
所选项目在 .Net 1.1 中运行良好,但在 3.5 中运行良好。我在遇到问题的地方提供示例代码。相同的代码在 .Net1.1 中运行良好
下面是aspx行
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs" Inherits="WebApplication1.ListBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>UsageReport</title>
<script language="javascript">
function isAllChecked()
{
var ctrlList = document.getElementById("lstUsers");
var allChecked = document.getElementById("chkAll");
var checked = allChecked.checked;
if (checked) {
ctrlList.disabled = true;
unselectItems();
}
else {
ctrlList.disabled = false;
}
}
function unselectItems()
{
var lstUsers = document.getElementById("lstUsers");
var list = lstUsers;
for (var i = 0; i < list.length; i ++)
{
list[i].selected = false;
}
list.selectedIndex = -1;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table>
<tr>
<td>
</td>
<td>
<asp:CheckBox ID="chkAll" Checked="True" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ListBox ID="lstUsers" runat="server" SelectionMode="Multiple" Enabled="false"></asp:ListBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
</table>
</form>
</body>
</html>
下面是 CodeBehind 的行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class ListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.chkAll.Attributes["onclick"] = "isAllChecked();";
lstUsers.DataSource = new string[] { "one", "two", "three" };
lstUsers.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem li in lstUsers.Items)
{
if(li.Selected)
Response.Write(li.Value);
}
}
}
}