控件是对数据和方法的封装。控件可以有自己的属性和方法。属性是控件数据的简单访问者。方法则是控件的一些简单而可见的功能。
一、标准:
1、Button字面意思,这个再网站上很常用,提个交啦什么的都会用按钮,双击这个按钮控件会激发控件的事件οnclick="Button1_Click",不能随便双击控件,因为会激发事件,如果激发了事件在前台页面上有显示,只删除后台代码上的事件会报错。(进行模拟错误演示)。
※前面的object用来传递事件的发生者,后面的EventArgs用来传递事件的细节
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("我的第一个动态网页!");
}
Button的属性:大小字体大小等
2、checkbox
打开autopostback
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
Response.Write("123");
}
关掉autopostback,说一下顺序执行的问题
protected void Button1_Click(object sender, EventArgs e)
{
if (CheckBox1.Checked == true && CheckBox2.Checked==false)
{
Response.Write("您的性别是男生");
}
else if(CheckBox2.Checked == true && CheckBox1.Checked==false)
{
Response.Write("您的性别是女生");
}
else if(CheckBox1.Checked==false && CheckBox2.Checked==false)
{
Response.Write("我的第一个动态网页");
}
else if(CheckBox1.Checked==true && CheckBox2.Checked==true)
{
Response.Write("我的第一个动态网页");
}
3、DropDownList
向其中添加可选择的项,点编辑项,然后点击添加,text,value,然后enable表示是否可用,selected表示选·中哪一个。都是false的话默认是第一个。
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "0")
{
Response.Write("<script>alert ('请选择性别');location.herf='Default.aspx'</script>");
}
else if (DropDownList1.SelectedValue == "1")
{
Response.Write("您选择的性别是男生");
}
else if (DropDownList1.SelectedValue == "2")
{
Response.Write("您选择的性别是女");
}
}
3、imagebutton
绑定图片,想让按钮更漂亮,用这个,加工好的图片放进image文件夹,然后选择图片路径
4、label控件
静态的:是一个简单的控件,能进行文字的显示,margin-top是它相对上面那个控件的距离,设置格式:
<div style=' text-align:center; background-color:Silver; margin-top=50px'>
属性中backcolor是框内的背景色,div中的背景色是整个那div时的颜色,bordercolor,borderstyle,borderwith边框,颜色,形式,宽度
动态的:用几个控件进行组合,对一些信息
protected void Button1_Click(object sender, EventArgs e)
{
sex = this.DropDownList1.Text.ToString().Trim();
this.Label1.Text = sex;
}
5、linkbutton,和button类似,只是形状不同而已。可以激发事件
protected void LinkButton1_Click(object sender, EventArgs e) { Response.Write("<script>alert('123');location.href='Default.aspx'</script>");
}
6、拖出来两个radiobutton,然后改变他们的gropename属性,将两个属性改为一个名字。然后就可以二选一了。
静态打印显示:将autopostback调成true
然后双击激发事件,写代码:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
Response.Write("您的性别是男生");
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Response.Write("您的性别是女生");
}
动态演示:
//radiobutton演示
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
sex = this.RadioButton1.Text.ToString();
}
else
{
sex = this.RadioButton2.Text.ToString().Trim();
}
//this.Label1.Text = sex;
Response.Write("<script>alert('您的性别是"+sex+"');location.href='Default.aspx'</script>");
}
用radiobutton给某变量赋值。将autopostback关掉,然后写代码
声明string sex
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
//Response.Write("您的性别是男生");
sex = this.CheckBox1.Text.ToString().Trim();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(sex);
}
7、textbox
主要介绍属性enable;maxlength限制最大输入字符数;readonly规定是不是只读的,false可以写,true只能看不能写,可以用作展示文字用它比label好的地方在于,可以放下很多文字,使用下拉条。可以做到;textmode的几种形式,密码的,下拉条的,单一直线的.singleline
利用textbox进行数据的输入,并显示打印出来:
protected void Button1_Click(object sender, EventArgs e)
{
string name = this.TextBox1.Text.ToString();
Response.Write(name);
}
用textbox在数据库中进行数据插入
protected void Button1_Click(object sender, EventArgs e)
{
//string name = this.TextBox1.Text.ToString();
//Response.Write(name);
string name = this.TextBox1.Text.ToString().Trim();
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
SqlCommand cmd = new SqlCommand("insert into db_binddata (db_text) values ('" + name + "')",con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('插入数据" + name + "成功|');location.href='Default.aspx'</script>");
this.TextBox1.Text = "";
}
执行数据更新
把插入语句改为更新语句:
SqlCommand cmd = new SqlCommand("update db_binddata set db_text='"+name+"' where db_id=27",con);
二、数据控件
1、gridview(此为初学期重点控件)
Gridview:属性,datakeynames属性,这个属性很重要,如果不填写,会报很多神奇的错误,那错误报的你不知所措。所以这个属性一定要填,这个填写的是主键的列名。一般交什么什么id。为数据库自动填写的一列。第二对数据进行绑定,创建一个列,取消自动生成的列,然后编写代码,需要自定义函数bindGV()。拉一个gridview控件,然后点击编辑列,添加一个Hyperlink,一个boundField,一个编辑更新取消,一个删除。新建一个页,叫GVneirong.aspx
链接的属性,数据第一个写上主键的名字,第二个按下面的格式写上,第三个是单元格要显示的内容,我要显示数据库中id那一列,因此写id。最重要的是写上navigateurl,即跳到哪一页。然后写代码。然后转到事件,进行双击激发事件,进行代码编写。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGV();
}
}
//数据绑定
public void bindGV()
{
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from db_binddata", con);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
this.GridView1.DataSource=ds;
this.GridView1.DataBind();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;//gridview的editindex属性,获取或者设置要编辑行的索引,要获取编辑的值是从零开始的,默认值为-1,指示计算机没有可以编辑的行,于是就能取消了。
bindGV();
}
//编辑列控件
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindGV();
}
//删除行操作
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();/*获取所删除行的索引*/
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
//delete from 表名 where 列名='"+前面定义的在giridview中获取的控件+"'
SqlCommand cmd = new SqlCommand("delete from db_binddata where db_id='" + id + "'", con);
//SqlCommand cmd = new SqlCommand("select from tb_kind where k_id='" + id + "'", con);
cmd.ExecuteNonQuery();
con.Close();
bindGV();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string newtext = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
SqlCommand cmd = new SqlCommand("update db_binddata set db_text='" + newtext + "' where db_id='" + id + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bindGV();
}
//gridview分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGV();
}
}
内容接收页面的代码,建一个label改大点字体。统一写上第一次加载,模拟不是第一次加载会怎样。并讲解原因,数据库已经改了,因为没有加载。
public void Bindlabel()
{
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
string id = Request.QueryString["id"];
SqlDataAdapter sda = new SqlDataAdapter("select * from db_binddata where db_id ='" + id + "'", con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.Label1.Text = ds.Tables[0].Rows[0][1].ToString();
con.Close();
}
2、dropdownlist的数据绑定
public void bindddl()
{
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from db_binddata", con);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "db_text";
this.DropDownList1.DataValueField = "db_id";
this.DropDownList1.DataBind();
}
3、综合搜索
if (!IsPostBack)
{
//this.DropDownList1.Items.Insert(0, new ListItem("全部"));
bindddl();
this.DropDownList1.Items.Insert(0, new ListItem("全部"));
bindGV();
}
public void bindGV()
{
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123");
con.Open();
if (DropDownList1.SelectedItem.Text == "全部")
{
SqlDataAdapter sda = new SqlDataAdapter("select * from db_binddata", con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
else
{
string text = this.DropDownList1.SelectedItem.Text.ToString().Trim();
SqlDataAdapter sda = new SqlDataAdapter("select * from db_binddata where db_text='" + text + "'", con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
}
三、验证控件
1、requiredfieldvalidator需要一个数据,就是这个不能为空,是不能为空判断控件,做一个成功了得页面。写上属性controltovalidator属性,如果不写,报错,模拟一下这个错误。
2、RangeValidator范围验证控件,首先更改属性type,告诉计算机我验证的这个数据是什么类型的,选择integer,整形的,max和min设置上数字。
3、compareValidator比较控件,先进行数据类型检验设置属性,需要两个输入框textbox,选择属性,控制哪个控件,填上controltovalidate,选择type:integer,比较的类型,然后是operator数据符合的要求,在此选择datatypecheck。Contrltovaluedata
第二去验证数字,大于某个数,小于某个数,或怎样。让他大于18。设置type:integer设置operator:greaterthan设置valuetocompare:18。
第三是两个控件进行比较,这是最常用的,介绍textbox的密码用法,并介绍密码确认。
4、regularExpressionValidator正则表达式验证控件,设置validationExpression,对什么进行验证。在此演示对邮箱地址验证。
5、customValidator自定义验证控件。验证性别编写代码,agrs,有两个属性,一个是values,值属性,一个是isvalid属性后者是是否通过验证的意思有true和false,此时Button不能有事件,否则根本无法验证。原因不明。
protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
{
if (args.Value == "男" || args.Value == "女")
{
args.IsValid = true;(true 通过验证)
}
else { args.IsValid = false; }
}
6、ValidationSummary验证汇总,将display属性(控件显示方式)改成none(不显示)static(静态的,就是如果两个验证控件放到一行了,静态的放到哪里显示到哪里,不会动地方)dynamic(动态的)要想实现动静态,需要把所有的验证控件都设成静态或者动态才能看出效果,然后把errormassage改成应显示的错误信息,text是真正的错误信息显示的东西。
四、综合应用(登陆及注册页面)