问题
i am trying to get an alert that shows "Invalid is and password" if the login fails but the problem is how do i fire the alert from server side. i think i have to call the javascript function from server side. i tried it but it's not working.I also another alert for "Please Contact manager" Here is my code below. what is tried to do is invoke the button press from the server side to fire the alert but it was not working aswell
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login</title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/Style.css" rel="stylesheet" />
<style type="text/css">
#btnSubmit {
height: 38px;
width: 179px;
}
</style>
</head>
<body style="background-image: url('imp.jpg'); background-repeat: no-repeat; height: auto; width: auto;">
<form id="form1" runat="server">
<div class="panel-img ">
<img src="Panel.png" />
</div>
<div>
<div style="position: absolute; z-index: 1; top: 183px; left: -1px; width: 596px; height: 125px;" id="layer1">
<div class="modal-body" style="top: 0px; left: 678px; margin-left: 400px; margin-right: 0px; margin-top: 30px;">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<label for="username" class="control-label font-weight-bold">Username</label>
<asp:TextBox ID="TextBox1" runat="server" CssClass="form-control" placeholder="Employee ID"></asp:TextBox>
<span class="help-block"></span>
</div>
<div class="form-group">
<label for="password" class="control-label font-weight-bolder ">Password</label>
<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control " placeholder="Password"></asp:TextBox>
<div id="error" class="invalid-feedback"></div>
<asp:Label ID="lblerror" runat="server" Text="Label"></asp:Label>
<br />
</div>
<asp:Button ID="Button1" runat="server" Text="Login" Width="211px" CssClass=" btn btn-success btn-block " OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Manager Tab" CssClass="btn btn-danger btn-block" Width="215px" OnClick="Button2_Click" Height="38px" />
<br />
<button type="button" id="btnSubmit" class="btn btn-primary btn-block">
Forgot Password!</button>
<br />
<br />
<div id="myAlert" class="alert alert-danger collapse">
<a id="linkClose" href="#" class="close">×</a>
<strong>Info!</strong> Please Contact Manager
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnSubmit').click(function () {
$('#myAlert').show('fade');
setTimeout(function () {
$('#myAlert').hide('fade');
}, 2000);
});
$('#linkClose').click(function () {
$('#myAlert').hide('fade');
});
});
</script>
<script src="Scripts/bootstrap.min.js">
</script>
</body>
</html>
Here is the Server Side Code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
SqlCommand co = new SqlCommand();
SqlDataReader ds;
SqlParameter @p1, @p4;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
cn.Open();
co.Connection = cn;
co.CommandText = "select * from reg where Id='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'";
ds = co.ExecuteReader();
if (ds.Read())
{
// co.CommandText = "select * from res where Id='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'";
Response.Redirect("http://localhost:50892/home.aspx");
}
else
{
lblerror.Text = "Incorrect Id and Password";
}
}
catch (Exception e1)
{
Response.Write(e1.Message);
}
finally
{
cn.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
cn.Open();
co.Connection = cn;
co.CommandText = "select * from manager where Id='" + TextBox1.Text + "' and Password='" + TextBox2.Text + "'";
ds = co.ExecuteReader();
if (ds.Read())
{
Response.Redirect("http://localhost:50892/home2.aspx");
}
else
{
// lblerror.Text = "Incorrect Id and Password";
//ScriptManager.RegisterClientScriptBlock(this, "mykey", "myfunctionclick(;)", true);
// ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "script", "myfuntionclick();", true);
// ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('Invalid Password')", true);
// ClientScript.RegisterStartupScript(this.GetType(), "JSScript", RegisterClientScriptBlock
// ScriptManager.RegisterStartupScript(this,this.GetType(), "script", "myfuntionclick();", true);
}
}
catch (Exception e1)
{
Response.Write(e1.Message);
}
finally
{
cn.Close();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
}
}
回答1:
Have you considered sending back an response to your client?
When you can't find the user in DB send back an Response with status code 422 (unprocessable entity).
Imagine you have a form:
<form id='myForm'>
...
<button id='submit'>Submit</button>
</form>
Now, using ajax you submit your data
<script>
$('#submit').onClick(function(){
$.ajax({
method: "POST",
url: "/you/script/.aspx",
data: {
user_name: 'user_name_from_form',
password: 'password_from_form'
},
success: function (response) {
//do something if you want
},
error: function (error) {
//show your alert - status code 422 is an error
}
});
});
</script>
回答2:
Try this
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", alert("Invalid Password"));
OR
ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('Invalid Password')", true);
UPDATE: I am putting sample code where SM is working on button click..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TESTSM.aspx.cs" Inherits="TESTSM" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" />
</Scripts>
</asp:ScriptManager>
<div>
<asp:Button ID="Button1" runat="server" Text="CLICK" ValidationGroup="vgTest" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
CODEBehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TESTSM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('Invalid Password')", true);
}
}
来源:https://stackoverflow.com/questions/60754050/bootstrap-alert-from-server-side-asp-net-using-javascript