我爱Aspx >> C#.Net >> 基于ASP.NET技术的驾校网页设计2、xy_search_win.aspx
在该页面中显示查询结果,如图2所示,此外还可以选中任一条记录进行删除和编辑操作。注意:在DataGrid1的”属性生成器”中添加‘删除’和‘选择’列,并将选择列的文本设成‘编辑’。

图2
xy_search_win.aspx.cs代码如下:
| using System.Data.SqlClient;//添加命名空间public class xy_search_win : System.Web.UI.Page{ public xy_search sourcepage; //定义第一个页面为当前页面的源页面 public string name1 { get { return DataGrid1.Items[DataGrid1.SelectedIndex].Cells[2].Text.ToString(); } }//定义所选行的‘学号’为公共变量 private void search1()//定义一个查询子程序 { SqlConnection myConnection= new SqlConnection ("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open();//连接数据库jx SqlDataAdapter adapter1= new SqlDataAdapter ("select 学号,姓名,报考车种,联系方式,证件号码,培训方式,欠款金额,备注 from V学员资料 "+ TextBox1.Text + " order by 学号", myConnection); //查询视图 DataSet myDataSet=new DataSet(); Adapter1.Fill(myDataSet,"result1"); DataGrid1.DataSource=myDataSet.Tables["result1"]; DataGrid1.DataKeyField="学号";//指定关键字段为学号 DataGrid1.DataBind();//绑定数据 myConnection.Close(); } private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { sourcepage=(xy_search)Context.Handler; // 使用Context.Handler属性来获得前一个页面实例对象的引用 TextBox1.Text=sourcepage.name1;//将第一个页面传过来的查询语句赋给TextBox1 search1();//调用查询子程序显示查询结果 } } private void DataGrid1_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)//”删除”按钮操作代码 { SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx"); myConnection.Open(); string mydelete="delete from 学员资料 where 学号='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";//删除“学员资料”表中‘学号’等于所选行对应的学号 SqlCommand myCommand=new SqlCommand(mydelete, myConnection);//创建myCommand对象 myCommand.ExecuteNonQuery();//执行sql语句 myConnection.Close(); search1();//重新调用子程序刷新页面; } private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e) //”编辑”按钮命令 { Server.Transfer("xy_search_detail.aspx"); } //转到第三个页面进行详细编辑}
Ҷƪл˵?
һƪ用.NET Framework 2.0创建Form设计器
|