62,075
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="Button" runat="server" Text="Button" onclick="Button_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
List<string> items = new List<string>();
items.Add("请选择...");
items.Add("祖国");
items.Add("万岁");
for (int i = 0; i < items.Count; i++)
{
this.DropDownList1.Items.Add(new ListItem(items[i]));
}
this.DropDownList1.SelectedIndex = 0;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = this.DropDownList1.SelectedIndex;
string textValue = "你选择了DropDownList1的Index是:" + selectedIndex;
ListItem item = new ListItem(textValue);
if (!DropDownList2.Items.Contains(item))
{
DropDownList2.Items.Add(item);
}
DropDownList2.Text = textValue;
}
}