一个小例子,让自己熟练下异步的写法,dropdownlist的前台清除选项,添加选项,checkboxlist的前台取值 前台: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 runat="server"> <title>一个小例子,让自己熟练下异步的写法,dropdownlist的前台清除选项,添加选项,checkboxlist的前台取值</title> <script type="text/javascript" language="javascript" src="jquery-1[1].3.2.min.js"></script> <script type="text/javascript"> function aaa() { d=document.getElementById('<%= DropDownList1.ClientID %>'); d.options.length=0; var ckelems = document.getElementById("CheckBoxList1").getElementsByTagName("input"); var spelems = document.getElementById("CheckBoxList1").getElementsByTagName("label"); for(var i=0;i<ckelems.length;i++) { if(ckelems[i].type=="checkbox") { if(ckelems[i].checked) d.options.add(new Option(spelems[i].innerHTML,spelems[i].innerHTML)); } } $.ajax( { type:'POST', url:'Default.aspx', dataType:'text', data:{Action:'test'}, cache: false, success:bbb } ) } function bbb(msg) { debugger; alert(msg); //alert("aaa"); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> <asp:ListItem>111</asp:ListItem> <asp:ListItem>222</asp:ListItem> </asp:DropDownList> <asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem Value="10">1个</asp:ListItem> <asp:ListItem Value="20">2个</asp:ListItem> <asp:ListItem Value="30">3个</asp:ListItem> <asp:ListItem Value="40">4个</asp:ListItem> </asp:CheckBoxList><br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="aaa();return false;" /><%-- OnClick="Button1_Click"--%> <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /></div> </form> </body> </html> 后台: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request["Action"] == "test" ) { string msg = string.Empty; msg = "true"; Response.Clear(); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/text"; Response.Write(msg); Response.End(); } } protected void Button1_Click(object sender, EventArgs e) { int temp = this.DropDownList1.Items.Count; for (int j = 0; j < temp; j++) { this.DropDownList1.Items.RemoveAt(0); } for (int i = 0; i < this.CheckBoxList1.Items.Count; i++) { if (this.CheckBoxList1.Items[i].Selected) { this.DropDownList1.Items.Add(new ListItem((i+1)+"g")); } } } protected void Button2_Click(object sender, EventArgs e) { Response.Write("aaa"); } protected void btntest_ServerClick(object sender, EventArgs e) { Response.Write("bbb"); } }