使用JavaScript从服务器端asp.net引导警报 [英] bootstrap alert from server side asp.net using javascript

查看:93
本文介绍了使用JavaScript从服务器端asp.net引导警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取显示"无效的是密码",如果登录失败,但问题是我该如何从服务器端发出警报.我认为我必须从服务器端调用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 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" />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                            <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">&times;</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>

这是服务器端代码

 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)
    {

    }
}


推荐答案

尝试一下

ClientScript.RegisterStartupScript(this.GetType(), "JSScript",  alert("Invalid Password"));

ScriptManager.RegisterStartupScript(Page, this.GetType(), "Exception", "alert('Invalid Password')", true);

更新:我将示例代码放在SM进行按钮单击的位置.

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);
    }

}

这篇关于使用JavaScript从服务器端asp.net引导警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆