(2)在Default.aspx 的页面上放置一个Grid View 控件。
(3)放置3个文本框并且放置3个Label控件设置其Text属性分别为“学生编号”、“学生姓名”和“所在班级”。
(4)放置3个RadioButton按钮设置其Text属性为“按编号查询”、“按姓名查询”和“按班级查询”,并且将GroupName属性值统一修改 为“a”。
(5)放置4个Button按钮设置其Text属性为“添加”、“删除”、“修改”、“查询”和“刷新”。添加控件后的页面运行效果如图7.2所示。
注意:在分别执行完“添加”、“删除”和“修改”按钮后再按“刷新”按钮,其目的是为了实现将更新后的信息显示到GridView控件中。
(6)主要程序代码如下。
自定义aa方法,该方法实现填充GridView控件的功能,代码如下。
public void aa()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=sa;database=stu;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from student";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
con.Open();//打开数据库连接
DataSet ds = new DataSet();
sda.Fill(ds, "student");//用student表填充数据集
con.Close();//关闭数据库连接
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
在Page_load事件中,调用aa方法,代码如下。
protected void Page_Load(object sender, EventArgs e)
{
this.aa();//调用aa方法
}
单击“添加”按钮用于添加学生信息,在该事件中用到INSERT插入语句将信息添加到数据库中,“添加”的程序代码如下。
protected void Button1_Click(object sender, EventArgs e)