设计一个选课程序,要求:该选课的运行界面包含2个列表框,左边为已开设的课程名称,通过form loa
设计一个选课程序,要求:该选课的运行界面包含2个列表框,左边为已开设的课程名称,通过formload事件加载,当单击某课程名称后,将该课程加入到右边的列表框中,并在左边的...
设计一个选课程序,要求:该选课的运行界面包含2个列表框,左边为已开设的课程名称,通过form load事件加载,当单击某课程名称后,将该课程加入到右边的列表框中,并在左边的列表框中删除该课程,当右边的课程数已满五门时,不允许再加入
展开
1个回答
展开全部
左边绑定课程名称,单击后把选中的加入右边列表,然后删除选择项,当加入右边列表时,首先判断是不是已满五门课程了
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Items.Clear();
DataTable dt = Common.SqlHelper.GetDataTabel("select * from tb_Course");
ListBox1.DataSource = dt;
ListBox1.DataTextField = "CourseName";
ListBox1.DataValueField = "ID";
ListBox1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ListBox2.Items.Count >= 5)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"","alert('已经选够5门了')",true);
return;
}
if (ListBox2.Items.Count > 0)
{
int count = 0;
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox1.SelectedValue.ToString() == ListBox2.Items[i].Value.ToString())
{
count = 1;
break;
}
}
if (count == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('已经选过这门了')", true);
}
else
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.ToString(), ListBox1.SelectedValue.ToString()));
ListBox1.Items.Remove(ListBox1.Items.FindByValue(ListBox1.SelectedValue.ToString()));
}
}
else
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.ToString(), ListBox1.SelectedValue.ToString()));
ListBox1.Items.Remove(ListBox1.Items.FindByValue(ListBox1.SelectedValue.ToString()));
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Items.Clear();
DataTable dt = Common.SqlHelper.GetDataTabel("select * from tb_Course");
ListBox1.DataSource = dt;
ListBox1.DataTextField = "CourseName";
ListBox1.DataValueField = "ID";
ListBox1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ListBox2.Items.Count >= 5)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"","alert('已经选够5门了')",true);
return;
}
if (ListBox2.Items.Count > 0)
{
int count = 0;
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox1.SelectedValue.ToString() == ListBox2.Items[i].Value.ToString())
{
count = 1;
break;
}
}
if (count == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('已经选过这门了')", true);
}
else
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.ToString(), ListBox1.SelectedValue.ToString()));
ListBox1.Items.Remove(ListBox1.Items.FindByValue(ListBox1.SelectedValue.ToString()));
}
}
else
{
ListBox2.Items.Add(new ListItem(ListBox1.SelectedItem.ToString(), ListBox1.SelectedValue.ToString()));
ListBox1.Items.Remove(ListBox1.Items.FindByValue(ListBox1.SelectedValue.ToString()));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询