GridView操作
来源:互联网 发布:知之深爱之切内容摘要 编辑:程序博客网 时间:2024/05/16 12:13
里面包含了GridView大部分的操作
并且配合Dropdownlist 和 联动
编辑时的联动定位可能写的不是很好
忘有大神提出优化
using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace TestSQL{ public partial class index1 : System.Web.UI.Page { string _connectionString = ConfigurationManager.AppSettings["ConnectionString"]; protected void Page_Load(object sender, EventArgs e) { /* if (HttpContext.Current.Session["UserName"] == null) { Response.Write("<script>alert('非法登录!请重新登录');window.location.href='Login.aspx'</script>"); //Response.Redirect("Login.aspx", false); }*/ // SessionLogin.Login1(); if (!IsPostBack) { this.Bind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //HiddenField hd = ((Label)GridView1.Rows[e.NewEditIndex].FindControl("Label2")).Text; // string b = ((Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label3"))).Text.ToString(); string Sname = (GridView1.Rows[e.NewEditIndex].FindControl("Label2") as Label).Text;//获取label的值 string ClassN = (GridView1.Rows[e.NewEditIndex].FindControl("Label3") as Label).Text;//获取label的值 GridView1.EditIndex = e.NewEditIndex; this.Bind(); DropDownList ddl = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"); ddl.SelectedValue = Sname; SqlConnection conn = new SqlConnection(_connectionString); conn.Open(); DropDownList DropDownList1 = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"); //得到当前的DropDownList string Specialty_Name = DropDownList1.SelectedValue; string strSQL = "SELECT class_name FROM class WHERE Specialty_ID=(SELECT ID FROM Specialty WHERE Specialty_Name='" + Specialty_Name + "' ) "; DataSet ds = new DataSet(); DataTable dt = new DataTable(); SqlDataAdapter adp = new SqlDataAdapter(strSQL, conn); adp.Fill(ds, "Class"); dt = ds.Tables["Class"]; GridViewRow dvr = (GridViewRow)DropDownList1.NamingContainer; DropDownList DropDownList2 = (DropDownList)dvr.FindControl("DropDownList2"); DropDownList2.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { DropDownList2.Items.Add(dt.Rows[i]["class_name"].ToString()); } conn.Close(); DropDownList2.SelectedValue = ClassN; } protected void Bind() { DataSet ds = new DataSet(); SqlConnection sqlconn = new SqlConnection(_connectionString); SqlDataAdapter sqld = new SqlDataAdapter("select * from student", sqlconn); sqld.Fill(ds, "tabstudent"); GridView1.DataSource = ds.Tables["tabstudent"].DefaultView; GridView1.DataBind(); Label1.Text = "查找成功"; } //更新操作 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { SqlConnection conn = new SqlConnection(_connectionString); DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1"); string Specialty_Name = ddl.SelectedValue; DropDownList ddl2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2"); string Class_Name = ddl2.SelectedValue; string strSQL = "update student set student_name=" +"'" +((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "'" +",Sex=" + "'" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "'" + ",Specialty=" + "'"+Specialty_Name+"'" + ",Class=" + "'" + Class_Name + "'" +"where Student_id=" + "'"+GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; SqlCommand cmd = new SqlCommand(strSQL, conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; Bind(); Label1.Text = "更新成功"; } //GridView取消按钮操作 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; this.Bind(); } //GridView删除操作 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string a = GridView1.Rows[e.RowIndex].Cells[4].Text; string b = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label2"))).Text; SqlConnection conn = new SqlConnection(_connectionString); string strSQL = "DELETE Student WHERE Student_id = " + "'" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; SqlCommand cmd = new SqlCommand(strSQL, conn); conn.Open(); //cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; Bind(); Label1.Text = "删除成功"; } //专业DropDownlist绑定数据 public DataSet ddlBind() { SqlConnection conn = new SqlConnection(_connectionString); conn.Open(); SqlDataAdapter adp = new SqlDataAdapter("SELECT Specialty_Name FROM Specialty ",conn); DataSet ds = new DataSet(); adp.Fill(ds, "Specialty"); conn.Close(); return ds; } //下拉变化事件,级联的实现效果 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(_connectionString); conn.Open(); DropDownList DropDownList1 = (DropDownList)sender; //得到当前的DropDownList string Specialty_Name = DropDownList1.SelectedValue; string strSQL = "SELECT class_name FROM class WHERE Specialty_ID=(SELECT ID FROM Specialty WHERE Specialty_Name='" + Specialty_Name + "' ) "; DataSet ds = new DataSet(); DataTable dt = new DataTable(); SqlDataAdapter adp = new SqlDataAdapter(strSQL, conn); adp.Fill(ds, "Class"); dt = ds.Tables["Class"]; GridViewRow dvr = (GridViewRow)DropDownList1.NamingContainer; //获取对DropDownList的容器引用 //得到dvr后就好办了查找下面的子控件。 DropDownList DropDownList2 = (DropDownList)dvr.FindControl("DropDownList2"); DropDownList2.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { DropDownList2.Items.Add(dt.Rows[i]["class_name"].ToString()); } //DropDownList2.SelectedIndex = DropDownList1.SelectedIndex; conn.Close(); } public void ReadLoadDroplist() { } protected void Button1_Click(object sender, EventArgs e) { DropDownList ddl = (DropDownList)GridView1.Rows[0].FindControl("DropDownList1"); ddl.SelectedValue = "Android開發與應用"; } }}
原创文章
0 0
- GridView操作
- GridView 操作
- GridView 操作
- GridView操作
- GridView操作
- GridView 操作
- GridView操作
- GridView操作
- GridView的常用操作
- GridView 18种操作
- GridView 18种操作
- GridView的几个操作
- GridView 18种操作
- GridView 18种操作
- GridView操作初步
- GridView中的DropDownList操作
- GridView 操作汇总
- GridView的操作大全
- mysql服务器安装
- 【手把手教你全文检索】Apache Lucene初探
- PPP 拨号
- 一维RMQ和二维RMQ模板以及用法
- 为什么不在ssh的action层做事务处理
- GridView操作
- 行内元素和块状元素
- Activity的启动模式
- java设计模式-接口适配器模式
- JAVA后台验证身份证是否合法
- unity 一个物体相对另一个物体坐标系的偏转角度计算
- python实现文本文件转二进制文件(二进制序列化)
- 重新启航,拥抱u3d
- Visual Studio VS2010统计代码行数