string query = "Select * from AdminLogin where username='" + name +
"' and password='" + password + "'";
DataSet ds = BusinessLogic.returnDataSet(query);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr[0].ToString() == name && dr[1].ToString() == password)
{
Response.Redirect("~/Home.aspx");
}
else
{
//Here I want to write the code that will open a message box
//that will tell to user that username and password does not match.
}
}
答案 0 :(得分:1)
通过消息框我假设您的意思是javascript警报。我不喜欢回复javascript函数。我认为它很混乱,只有在处理客户端操作时才应该使用javascript。
我实际上建议使用占位符和文字控件。您可以在网络表单中使用以下内容:
<asp:placeholder id="phLoginFailed" runat="server" visible="false">
<div class="loginfailed">
Login failed
</div>
</asp:placeholder>
此占位符可以像弹出窗口一样设置样式,也可以使用CSS在页面中显示。
然后将您的C#更改为:
else
{
phLoginFailed.Visible = true;
}
另外,值得一提的是,您的SQL查询很容易SQL Injection。您应该使用parameterised queries。
出于安全考虑,您应该在将密码存储到数据库时加密密码。
答案 1 :(得分:0)
这并不像听起来那么容易。基本上,您有两种选择:
向客户端发送一些JavaScript(例如,使用RegisterClientScriptBlock)调用JavaScript alert(...);
方法。
或者,使用“看起来像”弹出窗口的ASP.NET组件。一个例子是ASP.NET Ajax控件工具包中的ModalPopup component。
答案 2 :(得分:0)
只需将此行写在您想要显示消息的位置
this.Page.RegisterClientScriptBlock(Page.GetType(),"key", "alert('Wrong username or password')",true);
已编辑的代码
if (dr[0].ToString() == name && dr[1].ToString() == password)
{
Response.Redirect("~/Home.aspx");
}
else
{
this.Page.RegisterClientScriptBlock(Page.GetType(),"key", "alert('Wrong username or password')",true);
//Here I want to write the code that will open a message box
//that will tell to user that username and password does not match.
}
答案 3 :(得分:0)
ClientScript.RegisterClientScriptBlock(Page.GetType(),"key", "alert('Wrong username or password')", true);
或者如果在页面范围之外使用它 那么
Page page = (HttpContext.Current.Handler as Page);
if (page!=null)
{
page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "key", "alert('Wrong username or password')", true);
}