Bootstrap Alert From Server Side Asp.net Using Javascript
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 javascri
Solution 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>
Solution 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);
}
}
Post a Comment for "Bootstrap Alert From Server Side Asp.net Using Javascript"