仿照QQ添加好友页面 .NET代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
无标题页 C#代码: 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; public partial class _Default : System.Web.UI.Page { static int i = 0; protected void Page_Load(object sender, EventArgs e) { } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { } protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { ListBox2.Items[i].Text = ListBox1.Text; i++; } } 运行截图: 1. 先在设计页面中添加两个ListBox,分别为ListBox1,ListBox2.然后在ListBox1的编辑项里添加6个好友姓名,在ListBox2的编辑项里添加11个空ListItem。 2. 在设计页面中添加一个按钮。 ListBox2.Items[i].Text = ListBox1.Text;通过分析可知,单击LisBox1中的姓名选项,然后在单击确定按钮,选中的姓名则会在ListBox2中显示。该结果,是由单击按钮事件引起的,故:在protected void Button1_Click(object sender, EventArgs e)函数中写入ListBox2.Items[i].Text = ListBox1.Text;语句。要想能够实现添加多个好友,则还需要在该语句下面写入 i++;ListBox2中由11个空ListItem,即可以添加11个姓名。