📄 editteacher.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SMS;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
OPDataBase OP = new OPDataBase();
static string Sql = "select Age,Sex,Name,Address,DepartmentID,Phone from Teacher ";
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Attributes.Add("style", "table-layout:fixed");
if (!IsPostBack)
{
bindate();
}
if ((int)Session["Power"] != 0)
{
GridView1.Columns[6].Visible = false;
GridView1.Columns[7].Visible = false;
}
}
public void bindate()
{
SqlConnection conn = new SqlConnection(OP .getConnection ());
conn.Open();
//string sql = "select *from Teacher";
// string x = "Name";
SqlDataAdapter da = new SqlDataAdapter(Sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "Name" };
GridView1.DataBind();
conn.Close();
//OP.BinData(sql,GridView1 );
//GridView1.DataKeyNames =new string []{x};
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
bindate();
}
catch { }
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindate();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindate();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection( OP .getConnection ());
string sqlstr = "update Teacher set Name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',Age='"
+ ((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() + "',Phone='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Address='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',DepartmentID='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' where Name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "'";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
bindate();
}
catch (SqlException ex)
{
string a = ex.Message .ToString();
Label2.Text = a;
}
conn.Close();
}
protected void chaxun_btn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(OP.getConnection());
Sql= "select *from Teacher where "+DropDownList1 .SelectedValue .ToString ()+"='"+Infor_box .Text .Trim ()+"'";
try
{
OP.BinData(Sql ,GridView1 );
}
catch ( Exception ex1)
{
string a = ex1.Message.ToString();
Label2.Text = a;
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = OP.sqlConnection();
string sql = "delete from Teacher where Name='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(sql , conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
bindate ();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//((ImageButton)(e.Row.Cells[9].Controls[0])).Attributes.Add("onclick", "return confirm('你确认要删除吗?')");
//如果是绑定数据行 //清清月儿http://blog.csdn.net/21aspnet
}
}
protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{
Sql = "select Age,Sex,Name,Address,DepartmentID,Phone from Teacher ";
bindate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -