📄 moas1104.aspx.cs
字号:
using System;
using System.Data.SqlClient;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MOAS
{
/// <summary>
/// WebForm6 的摘要说明。
/// </summary>
public class WebForm6 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.LinkButton Linkbutton1;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.Label Label12;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.HtmlControls.HtmlInputButton button1;
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
// 在此处放置用户代码以初始化页面
//为按钮命名
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
Linkbutton1.Text="OK";
//调用BindGrid()函数
BindGrid();
//初始化画面时定位焦点
Page.RegisterStartupScript("focus", "<script language=javascript>document.getElementById('TextBox1').focus();</script>");
//将所属部门下拉列表框绑定数据库
string sql="select * from CODEMASTER where ID='C002'";
CCoSearchFromDatabase a=new CCoSearchFromDatabase();
DropDownList1.DataSource=a.getDataSet(sql);
//将职务下拉列表框绑定数据库
string sql1="select * from CODEMASTER where ID='C009'";
CCoSearchFromDatabase b=new CCoSearchFromDatabase();
DropDownList2.DataSource=b.getDataSet(sql1);
//将用户状态下拉列表框绑定数据库
string sql2="select * from CODEMASTER where ID='C003'";
CCoSearchFromDatabase c= new CCoSearchFromDatabase();
DropDownList3.DataSource=c.getDataSet(sql2);
//对与数据库的连接进行判断
if(a.getDataSet(sql)!=null&&b.getDataSet(sql)!=null&&c.getDataSet(sql)!=null)
{
//获取为下拉列表框的列表项提供文本内容的数据源字段
DropDownList1.DataTextField="VALUE";
//获取为下拉列表框的列表项提供值的数据源字段
DropDownList1.DataValueField="NO";
//将取到的数据填入下拉列表框
DropDownList1.DataBind();
//在下拉列表框中添加空白项
DropDownList1.Items.Add("");
//初始化画面时显示空白项
DropDownList1.Items.FindByText("").Selected=true;
//获取为下拉列表框的列表项提供文本内容的数据源字段
DropDownList2.DataTextField="VALUE";
//获取为下拉列表框的列表项提供值的数据源字段
DropDownList2.DataValueField="NO";
//将取到的数据填入下拉列表框
DropDownList2.DataBind();
//在下拉列表框中添加空白项
DropDownList2.Items.Add("");
//初始化画面时显示空白项
DropDownList2.Items.FindByText("").Selected=true;
//获取为下拉列表框的列表项提供文本内容的数据源字段
DropDownList3.DataTextField="VALUE";
//获取为下拉列表框的列表项提供值的数据源字段
DropDownList3.DataValueField="NO";
//将取到的数据填入下拉列表框
DropDownList3.DataBind();
//在下拉列表框中添加空白项
DropDownList3.Items.Add("");
//初始化画面时显示空白项
DropDownList3.Items.FindByText("").Selected=true;
}
//发现错误,提示错误信息,并且中止程序
else
{
HttpContext.Current.Response.Redirect("ErrorMessage.aspx?message=程序出现错误,请检查与数据库的连接是否正确!");
}
}
}
//获取总的页数、单页显示的项数、用户在使用过程中选择的页数
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
try
{
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
MyDataGrid.SelectedIndex=0;
BindGrid();
ShowStats();
}
catch(Exception ex)
{
//根据实际情况显示相应错误的类型
HttpContext.Current.Response.Redirect("ErrorMessage.aspx?message="+ex.Message+"");
}
}
//实现全部检索
public void BindGrid()
{
string[] page={""+Session["page"]+""};
ExcuteSql(page);
MyDataGrid.SelectedIndex=0;
ShowStats();
}
public void ExcuteSql(string[] s)
{
string bb=s[0];
Session.Add("page",bb);
string select="";
//检索到USERS表和CODEMASTER表中相对应的内容
if(bb!="")
{
select="select a.id 职员ID,a.name 姓名,b.value 所属部门,c.value 职务,a.mail Email from users a, codemaster b,codemaster c where (a.job_id=c.no and c.id='C009')and(a.dep_id=b.no and b.id='C002')and("+bb+")";
}
else
{
select="select a.id 职员ID,a.name 姓名,b.value 所属部门,c.value 职务,a.mail Email from users a, codemaster b,codemaster c where (a.job_id=c.no and c.id='C009')and(a.dep_id=b.no and b.id='C002')";
}
CCoSearchFromDatabase dsc1=new CCoSearchFromDatabase();
//对与数据库的连接进行判断
if(dsc1.getDataSet(select)!=null)
{
MyDataGrid.DataSource=dsc1.getDataSet(select);
MyDataGrid.DataBind();
ShowStats();
if(MyDataGrid.Items.Count!=0)
{
MyDataGrid.SelectedIndex=0;
}
else
{
MyDataGrid.SelectedIndex=-1;
}
}
//发现错误,提示错误信息,并且中止程序
else
{
HttpContext.Current.Response.Redirect("ErrorMessage.aspx?message=程序出现错误,请检查与数据库的连接是否正确!");
}
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
//实现下一页功能
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
break;
//实现上一页功能
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
//实现最后一页功能
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
//实现第一页功能
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
}
private void ShowStats()
{
if(MyDataGrid.CurrentPageIndex==-1)
{
//画面初始化时显示索引为第一页
lblCurrentIndex.Text = "第 1 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}
else
{
//点击控制页码的按钮后显示的当前页的索引是第几页
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.MyDataGrid.SelectedIndexChanged += new System.EventHandler(this.MyDataGrid_SelectedIndexChanged);
this.Linkbutton1.Click += new System.EventHandler(this.Linkbutton1_Click);
this.Button1.ServerClick += new System.EventHandler(this.Button1_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void MyDataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void Linkbutton1_Click(object sender, System.EventArgs e)
{
//发现异常
try
{
if(MyDataGrid.SelectedIndex==-1)
{
}
else
{
//将选中的用户的ID传到MOAS1105画面
Session.Add("strTransUseridFrom1104",MyDataGrid.Items[MyDataGrid.SelectedIndex].Cells[1].Text);
Response.Redirect("MOAS1105.aspx",false);
}
}
//处理异常
catch(Exception ex)
{
//根据实际情况显示相应错误的类型
HttpContext.Current.Response.Redirect("ErrorMessage.aspx?message="+ex.Message+"");
}
}
private void Button1_ServerClick(object sender, System.EventArgs e)
{
//发现异常
try
{
//显示全部信息
if(MyDataGrid.CurrentPageIndex!=0)
{
MyDataGrid.CurrentPageIndex=0;
BindGrid();
}
//按检索条件进行检索,显示部分满足条件的信息
if(MyDataGrid.CurrentPageIndex==0)
{
string[] strname={"a.id","a.name","a.dep_id","a.job_id","a.status","a.ip"};
//得到每一个字符串的值
string[] strvalue={""+TextBox2.Text+"",""+TextBox1.Text+"",""+DropDownList1.SelectedItem.Value+"",""+DropDownList2.SelectedItem.Value+"",""+DropDownList3.SelectedItem.Value+"",""+TextBox3.Text+""};
string strsql="";
int cnt=0;
for(int i=0;i<6;i++)
{
if(strvalue[i]!="")
{
if(cnt==0)
{
strsql=strname[i]+"='"+strvalue[i]+"'";
}
else
{
strsql=strsql+" and "+strname[i]+"='"+strvalue[i]+"'";
}
cnt=cnt+1;
}
else
{
}
}
string[] sss={""+strsql+""};
ExcuteSql(sss);
}
}
//处理异常
catch(Exception ex)
{
//根据实际情况显示相应错误的类型
HttpContext.Current.Response.Redirect("ErrorMessage.aspx?message="+ex.Message+"");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -