文本控件
1)ListBox列表框控件
在设计页面设计好,拖2个ListBox。1个label(显示你所选择的日期), 4个button(全往右,全往左,几个往右,几个往左,总共4个功能)
注意调整ListBox控件行为True
代码如下:
using System.Collections;
namespace WebApplication1
{
//listbox列表控件
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)//是否回传
{
ArrayList arrrayl = new ArrayList(); //实例化数组集合
arrrayl.Add("星期日"); //向集合中添加数据
arrrayl.Add("星期一");
arrrayl.Add("星期二");
arrrayl.Add("星期三");
arrrayl.Add("星期四");
arrrayl.Add("星期五");
arrrayl.Add("星期六");
ListBox1.DataSource = arrrayl; //数据来源绑定到Listbox控件中
ListBox1.DataBind();//执行绑定操作
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//ListBox1每一项的数据传输到ListBox2
int count = ListBox1.Items.Count;
//用循环遍历
for (int i = 0; i < count; i++)
{
//count的值就是ListBox1数据的个数,如上为7
ListItem item = ListBox1.Items[0];
ListBox1.Items.Remove(item); //把ListBox1的数据取出到ListBox2,并删除ListBox1的取出数据
ListBox2.Items.Add(item);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//ListBox2每一项的数据传输到ListBox2
int count = ListBox2.Items.Count;
//用循环遍历
for (int i = 0; i < count; i++)
{
//count的值就是ListBox1数据的个数,如上为7
ListItem item = ListBox2.Items[0];
ListBox2.Items.Remove(item); //把ListBox2的数据取出到ListBox1,并删除ListBox1的取出数据
ListBox1.Items.Add(item);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
Label1.Text = this.ListBox1.SelectedValue; //传输
int count = ListBox1.Items.Count;
int index = 0; //记录索引值,即数组的下标
//用循环遍历
for (int i = 0; i < count; i++)
{
//count的值就是ListBox1数据的个数,如上为7
ListItem item = ListBox1.Items[index]; //item是所选的数据数组(星期
if(ListBox1 .Items[index ].Selected ==true ) //如果数据被选择
{
ListBox1.Items.Remove(item); //把数据取出,并删除被取出的一边
ListBox2.Items.Add(item); //另一边数据加入
index--; //当前索引值减1
}
index++; //当前索引值加1
}
}
protected void Button4_Click(object sender, EventArgs e)
{
Label1.Text = this.ListBox2.SelectedValue; //传输
int count = ListBox2.Items.Count;
int index = 0; //记录索引值,即数组的下标
//用循环遍历
for (int i = 0; i < count; i++)
{
//count的值就是ListBox1数据的个数,如上为7
ListItem item = ListBox2.Items[index]; //item是所选的数据数组(星期
if (ListBox2.Items[index].Selected == true) //如果数据被选择
{
ListBox2.Items.Remove(item); //把数据取出,并删除被取出的一边
ListBox1.Items.Add(item); //另一边数据加入
index--; //当前索引值减1
}
index++; //当前索引值加1
}
}
}
}
2)DropDownLis控件
同样要注意更改控件行为为True
代码如下:
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//是否回传
{
ArrayList arrrayl = new ArrayList(); //实例化数组集合
arrrayl.Add("星期日"); //向集合中添加数据
arrrayl.Add("星期一");
arrrayl.Add("星期二");
arrrayl.Add("星期三");
arrrayl.Add("星期四");
arrrayl.Add("星期五");
arrrayl.Add("星期六");
DropDownList1.DataSource = arrrayl; //数据来源绑定到DropDownList1控件中
DropDownList1.DataBind();//执行绑定操作
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = this.DropDownList1.SelectedIndex;
string value = this.DropDownList1.SelectedValue;//把选择的值赋值给value
string text = this.DropDownList1.Items[index].Text;
this.Label1.Text = text;//把所取得的文本赋值给label
}
结果如图:
选择类型控件
1)RadioButton单选按钮控件
在设计页面设计好,拖四个radiobutton(即四个选项)。一个label(你选择的选项), 一个button(提交按钮)
其中注意设置好radiobutton中的三个值,GroupName 都为Key1 哦!
其中双击radiobutton中的checkedchar开始编写代码
之后还要记得双击Button编写其提交后的代码
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
this.Label1.Text = "A";
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked == true)
{
this.Label1.Text = "B";
}
}
protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton3.Checked == true)
{
this.Label1.Text = "C";
}
}
protected void RadioButton4_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton4.Checked == true)
{
this.Label1.Text = "D";
}
}
//Button1的代码
protected void Button1_Click(object sender, EventArgs e)
{
if(RadioButton1 .Checked ==false && RadioButton2 .Checked == false &&RadioButton3 .Checked ==false &&RadioButton4 .Checked ==false)
{
Response.Write("<script>aler t('请选择答案')");//如果四个选项都没有写就会弹出对话框
}
else
if(RadioButton4 .Checked ==true)
{
Response.Write("<script>aler t('答对了,正确答案为D!')</script>");//如果选项D被选择就会弹出对话框提示正确
}
else
{
Response.Write("<script>aler t('正确答案为D,你错了!')</script>");
}
}
2)CheckBox 多选控件
label的Text为空,在网页就不会显示了
注意同radiobutton的设置
在双击其事件触发编写代码
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if(CheckBox1 .Checked)
{
this.Label1.Text = "A";
}
else
{
this.Label1.Text = " ";
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox2.Checked)
{
this.Label2.Text = "B";
}
else
{
this.Label2.Text = " ";
}
}
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox3.Checked)
{
this.Label3.Text = "C";
}
else
{
this.Label3.Text = " ";
}
}
protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox4.Checked)
{
this.Label4.Text = "D";
}
else
{
this.Label4.Text = " ";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if(CheckBox1 .Checked ==false && CheckBox2.Checked == false && CheckBox3.Checked == false && CheckBox4.Checked == false)
{
Response.Write("<script>aler t('请选择答案')");//如果四个选项都没有写就会弹出对话框
}
else
if (CheckBox1.Checked == true && CheckBox2.Checked == true && CheckBox3.Checked == true&& CheckBox4.Checked == false )
{
Response.Write("<script>aler t('恭喜你,答对了,正确答案为ABC!')</script>");//如果选项D被选择就会弹出对话框提示正确
}
else
{
Response.Write("<script>aler t('正确答案为ABC,你错了呦吼吼,四边形不一定就是正方形,也可能是长方形!')</script>");
}
}