asp.net是如何获得客户端的事件并以此调用服务端的方法的?
以用户登录界面为例说明一下吧:
当用户输入用户名和密码,然后点击登录按钮时,会触发函数,
登录按钮的代码如下:
<asp:ImageButton ID="btnLogin" runat="server" ImageUrl="Images/login_button1.png"
Width="67" Height="26" BorderWidth="0" OnClick="btnLogin_Click" />
所以触发的函数是OnClick="btnLogin_Click"
下面的函数考虑的比较详细,不需详细了解的。只知道在后台调用的是btnLogin_Click,就可以了。
protected void btnLogin_Click(object sender, EventArgs e)
{
string strUserName = Request.Form["UserName"].ToString().Trim().ToLower();
string strPassWord = Sha1.GetSha1(Request.Form["PassWord"].ToString().Trim());
string SqlString = "SELECT * FROM Users WHERE Login = '" + strUserName + "'";
DataTable dt = DataAccess.SelectBySql("SOC2.ConnectionString.Sql", SqlString);
//用户名存在,判断输入密码是否正确
if ((dt.Rows.Count == 1) && (dt.Rows[0][8].ToString() != "1"))
{
string SqlString1 = "Select * from Users Where Pass='" + strPassWord + "'";
DataTable dt1 = DataAccess.SelectBySql("SOC2.ConnectionString.Sql", SqlString1);
if (dt1.Rows.Count == 0)
{
Response.Write("<script language=javascript>window.alert('密码输入错误');location.href='UserLogin.aspx'</script>");
//获取当前尝试登录的用户的登录失败次数,并对其加1
if (!strUserName.Equals("admin"))
{
int i = Convert.ToInt32(dt.Rows[0][7]) + 1;
DataAccess.Update("SOC2.ConnectionString.Sql", "Update Users SET FailCount = " + i + " where Login = '" + strUserName + "'");
if (i >= 3)
{
DataAccess.Update("SOC2.ConnectionString.Sql", "Update Users SET IsLocked = 1 where Login = '" + strUserName + "'");
}
}
// 写入登录失败日志。
string sqlGetUserId = string.Format("SELECT ID from Users WHERE Login = '{0}'", strUserName);
dt = DataAccess.SelectBySql("SOC2.ConnectionString.Sql", SqlString);
if (dt.Rows.Count > 0)
{
WriteLoginLog(dt.Rows[0][0].ToString(), false);
}
}
else
{
Int64 loginId = WriteLoginLog(dt.Rows[0]["ID"].ToString(), true);
Session["LoginID"] = loginId;
Session["User_ID"] = (int)dt.Rows[0]["ID"];
Session["User_Name"] = strUserName;
Application[dt.Rows[0]["ID"].ToString()] = loginId;
string SqlString2 = "select Role_ID,Role.Name, Role.Permission from Role inner join Users on Role.ID = Users.Role_ID where Users.Login = '" + strUserName + "'";
DataTable dt2 = DataAccess.SelectBySql("SOC2.ConnectionString.Sql", SqlString2);
Session["User_Role"] = dt2.Rows[0]["Name"].ToString();
Session["Role_ID"] = (int)dt2.Rows[0]["Role_ID"];
Session["UserPermission"] = (UserPermission)(int)dt2.Rows[0]["Permission"];
Response.Redirect("Default.aspx");
}
}
//用户名不存在或帐号累计输入错误3次
else
{
if(dt.Rows.Count == 0)
{
Response.Write("<script language=javascript>window.alert('你输入的用户名不存在');location.href='UserLogin.aspx'</script>");
}
else
Response.Write("<script language=javascript>window.alert('你的帐号累计输入错误3次,已被锁定');location.href='UserLogin.aspx'</script>");
}
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}