DropDownList操作;ListBox操作;动态创建控件;Response.Write("欢迎学习ASP.NET''!");
1 DropDownList操作;
2 ListBox操作;
3 动态创建控件;
4 Response.Write("欢迎学习ASP.NET''!");
=====================
1 DropDownList操作;(向右选择,向上移,向下移选项;)
if(DropDown1.Items.Count > 1)
{
ListItem Item = DropDown1.SelectedItem;
DropDown1.Items.Remove(Item);
String val = DropDown1.SelectedItem.Value ;
String text = DropDown1.SelectedItem.Text;
Label1.Text = val + text;
}
else
{
DropDown1.Items.Clear();
Label1.Text = "";
}
2 ListBox操作;(向右选择,向上移,向下移选项;)
private void Movebtn_Click(object sender, System.EventArgs e)
{
int Count = ListBox1.Items.Count;
int Index = 0;
for(int i=0;i<Count-1;i++)
{
ListItem Item = ListBox1.Items[Index];
if(ListBox1.Items[Index].Selected == true)
{
ListBox1.Items.Remove(Item);
ListBox2.Items.Add(Item);
Index--;
}
Index++;
}
}
private void Upbtn_Click(object sender, System.EventArgs e)
{
//若不是第一行则上移
if( ListBox1.SelectedIndex > 0 )
{
string name = ListBox1.SelectedItem.Text;
string ID = ListBox1.SelectedItem.Value;
int index = ListBox1.SelectedIndex;
ListBox1.SelectedItem.Text = ListBox1.Items[index-1].Text;
ListBox1.SelectedItem.Value = ListBox1.Items[index-1].Value;
ListBox1.Items[index-1].Text = name;
ListBox1.Items[index-1].Value = ID;
ListBox1.SelectedIndex --;
}
}
private void Downbtn_Click(object sender, System.EventArgs e)
{
//若不是最后一行则下移
if( ListBox1.SelectedIndex >= 0 && ListBox1.SelectedIndex < ListBox1.Items.Count-1 )
{
string name = ListBox1.SelectedItem.Text;
string ID = ListBox1.SelectedItem.Value;
int index = ListBox1.SelectedIndex;
ListBox1.SelectedItem.Text = ListBox1.Items[index+1].Text;
ListBox1.SelectedItem.Value = ListBox1.Items[index+1].Value;
ListBox1.Items[index+1].Text = name;
ListBox1.Items[index+1].Value = ID;
ListBox1.SelectedIndex ++;
}
}
3 动态创建控件;
private void Addbtn_Click(object sender, System.EventArgs e)
{
DropDownList DropDown = new DropDownList();
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(DropDown);
DropDown.ID="ControlID";
DropDown.Width=200;
DropDown.Items.Add(new ListItem("北京","0"));
DropDown.Items.Add(new ListItem("上海","1"));
DropDown.Items.Add(new ListItem("河北","2"));
ViewState["AddControl"] = true;
}
4 Response.Write("欢迎学习ASP.NET''!");