搜索功能及用户登录

来源:百度文库 编辑:神马文学网 时间:2024/04/19 23:51:51
//搜索功能
1. public void searchEmp()
        {
          
            try
            {
               strkey = Request.QueryString["key"].ToString();
               ViewState["key"] =strkey;            }
            catch
            {
                return;
            }
            DataTable dt = _empBll.SearchFirstPage(strkey);
            this.rtEmpList.DataSource = dt;
            this.rtEmpList.DataBind();
            int totalCount = dt.Rows.Count;
            int lastPageIndex = totalCount % 20 == 0 ? totalCount / 20 : totalCount / 20 + 1;
            this.lblCurrentIndex.Text = "1";
            this.lblLastIndex.Text = lastPageIndex.ToString();
            this.lblTotalCount.Text = totalCount.ToString();        }
function getkey() {
    location.href = "Index.aspx?key="+document.getElementById("txtsearch").value;
}
"  style=" width:80px; height:14px" />
 public DataTable SearchFirstPage(string key)
       {
           return _pageringBll.FirstPage("employee",
               "emp_id",
               "jobs",
               "job_id",
               "emp_id,fname,lname,job_desc,min_lvl,max_lvl",
               "emp_id like '%" + key + "%' or fname like '%" + key + "%' or lname like '%" + key + "%' or job_desc like '%" + key + "%' or min_lvl like '%" + key + "%' or max_lvl like '%" + key + "%' and", 20);
       }2.第二种搜索方法把html标签变成asp标签用session对象实现
 protected void lbtsearch_Click(object sender, EventArgs e)
        {
            Session["key"] = this.txtSearch.Value;
            string str = Session["key"].ToString();
            DataTable dt = _empBll.SearchFirstPage(str);
            this.rtEmpList.DataSource = dt;
            this.rtEmpList.DataBind();
        }
                  搜索//ViewState :获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。
下面的示例演示如何实现从其控件的 ViewState 属性存储和检索值的 Text 属性。private const int defaultFontSize = 3;// Add property values to view state with set;
// retrieve them from view state with get.
public String Text
{
    get
    {
        object o = ViewState["text"];
        return (o == null)? String.Empty : (string)o;
    }    set
    {
        ViewState["Text"] = value;
    }
}  
//FormsAuthentication:用于在登录时验证用户的 Forms 身份验证凭据可以存储在外部数据源中,也可以存储在应用程序的配置文件中。ASP.NET 成员资格是在 Forms 身份验证应用程序中存储和管理用户凭据的首选方法
GetAuthCookie 已重载。 为给定的用户名创建身份验证 Cookie。
SetAuthCookie 已重载。 为提供的用户名创建一个身份验证票证,并将其添加到响应的 Cookie 集合或 URL。
SignOut 从浏览器删除 Forms 身份验证票证。     //大项目中验证用户登录
用控件的名字获取用户输入的用户名和密码
private string _pwd;
private string _userName;
private void ReceiveFromData()
{
try
{
_pwd=Request.From["txtPwd"];
_userName=Request.From["txtUserName"];
}
catch(Exeception ex)
{
this.lblError.Text=ex.Message;
}
}
private void ShowError(string error)
{
this.lblError.Text=error;
}
private void Login()
{
ReceiveFromData();
string str;
try
{
str=_userBll.Login(_userName,_pwd)
}
catch(Exeception ex)
{
str=ex.Message;
ShowError(str);
}
if (str!=string.Empty)
{
ShowError(str);
}
else
{
FormsAuthentication.SetAuthCookie(_userName,true);
Session["userName"]=_userName;
Response.Redirect("Index.aspx");
}
}
//存储过程
Create procedure SP_GetcountOfEmployee
@fname varchar(50),
@lname varchar(50)
AS
select count(emp_id) from employee where fname=@fname and lname=@lname
GO//DAL层
private string sp_GetcountOfEmployee="SP_GetcountOfEmployee";
public int GetcountOfEmployee(string fname,string lname)
{
try
{
return (int)SQLHelper.ExecuteScalar(DataProvider.connectionString,sp_GetcountOfEmployee,fname,lname);
}
catch(SqlException se)
{
trow se;
}
catch(Exception ex)
{
throw ex;
}
}
//BLL层
private UserDal _userDal;
public UserBll():this(new UserDal())
{}
public UserBll(UserDal userDal)
{
_userDal=userDal;
}
public string Login(string fname,string lname)//返回string类型
{
int count;
try
{
count=_userDal.GetcountOfEmployee(fname,lname);
}
catch(Exception ex)
{
throw ex;
}
switch(count)
{
case 1:
return String.Empty;//有返回类型时不用break;
case 0:
return "用户名或密码错误!";
default:
return "账户异常,请与系统管理员联系";
}