📄 teacher_view.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 System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class teacher_teacher_view : System.Web.UI.Page
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
private string strConnect = ConfigurationSettings.AppSettings["ConnectionString"];
int startIndex = 0;
string id = "";
string name = "";
string dept = "";
string strCmd = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownListDataBind();
dept = "";
name = TextBox1.Text;
id = TextBox2.Text;
strCmd = createSQL(dept, name, id);
DataGridDataBind(strCmd);
}
}
private string createSQL(string dept, string name, string id)
{
string sqlwhere = "";
if (!dept.Equals(""))
{
sqlwhere += " AND 所在单位 LIKE '%" + dept + "%'";
}
if (!name.Equals(""))
{
sqlwhere += " AND 姓名 LIKE '%" + name + "%'";
}
if (!id.Equals(""))
{
sqlwhere += " AND 教师编号 LIKE '%" + id + "%'";
}
string str1 =
"SELECT * " +
"FROM H_人事核心视图 WHERE 1=1" + sqlwhere+" order by 教师编号";
return str1;
}
private void DataGridDataBind(string strCmd)
{
//创建数据适配器对象
SqlConnection objConnection = new SqlConnection(strConnect);
//objCommand.Connection = objConnection;
SqlDataAdapter da = new SqlDataAdapter(strCmd, objConnection);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//从指定的索引开始取PageSize条记录
da.Fill(ds, startIndex, DataGrid1.PageSize, "CurDataTable");
//填充数据集
da.Fill(ds, "AllDataTable");
//设置DataGrid控件实际要显示的项数
DataGrid1.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
DataGrid1.DataSource = ds.Tables["CurDataTable"];
DataGrid1.DataBind();
}
catch (Exception error)
{
Response.Write(error.ToString());
}
finally
{
objConnection.Close();
ds.Clear();
}
}
private void DropDownListDataBind()
{
//创建数据适配器对象
SqlConnection objConnection = new SqlConnection(strConnect);
//objCommand.Connection = objConnection;
SqlDataAdapter da = new SqlDataAdapter("select * from H_Code_所在单位", objConnection);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//从指定的索引开始取PageSize条记录
da.Fill(ds, "CurDataTable");
//填充数据集
da.Fill(ds, "AllDataTable");
//设置DataGrid控件实际要显示的项数
DataGrid1.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
DropDownList1.DataSource = ds.Tables["CurDataTable"];
DropDownList1.DataTextField = "content";
DropDownList1.DataValueField = "content";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "");
}
catch (Exception error)
{
Response.Write(error.ToString());
}
finally
{
objConnection.Close();
// ds.Clear();
}
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
//DataGrid1.DataSource = null;
//DataGrid1.DataBind();
//设置DataGrid当前页的索引值为用户选择的页的索引
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex;
//重新绑定数据
dept = this.DropDownList1.SelectedItem.Text; ;
name = TextBox1.Text;
id = TextBox2.Text;
strCmd = createSQL(dept, name, id);
DataGridDataBind(strCmd);
// DataGridDataBind(strCmd);
Label1.Text = strCmd + "------------------";
Label2.Text = DataGrid1.CurrentPageIndex.ToString();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
id = TextBox2.Text;
name = TextBox1.Text;
dept = this.DropDownList1.SelectedItem.Text;
strCmd = createSQL(dept, name, id);
Label1.Text = strCmd;
//设置当前的页为0
DataGrid1.CurrentPageIndex = 0;
Label2.Text = DataGrid1.CurrentPageIndex.ToString();
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex;
DataGridDataBind(strCmd);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -