Bonjour, je travaille sur un projet simple utilisant javascript, et j'essaie de déclencher un événement de clic après que la page soit renvoyée du serveur, et je n'arrive pas à comprendre comment le faire. Voici mon code ci-dessous

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .SignUp
        {
            padding-right: 450px;
            padding-left: 25px;
            margin-top: 100px;
        }
        .auto-style1 
        {
            color: #FFFFFF;
        }
        .auto-style2
        {
            text-align: left;
        }
        .auto-style3 
        {
            color: #FFFFFF;
            width: 158px;
        }
        .Message
        {
            margin-left: 100px;
            margin-top: 75px;
        }
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h1>Sign Up for an Account:</h1>
    <div class="SignUp">
        <table class="auto-style1">
            <tr>
                <td class="auto-style3">First Name</td>
                <td class="auto-style2">
                    <asp:TextBox ID="FirstName" Class="FirstName" runat="server" style="margin-left: 0px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Firstname" ErrorMessage="Please Enter First Name" ForeColor="Red">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">Last Name</td>
                <td class="auto-style2">
                    <asp:TextBox ID="LastName" class="LastName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter Last Name" ForeColor="Red" ControlToValidate="LastName">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">Password</td>
                <td class="auto-style2">
                    <asp:TextBox ID="Password" class="Password" runat="server" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Create a Password" ForeColor="Red" ControlToValidate="Password">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">Confirm Password</td>
                <td class="auto-style2">
                    <asp:TextBox ID="ConfirmPassword" class="ConfirmPassword" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Confirm Password" ForeColor="Red" ControlToValidate="ConfirmPassword">*</asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" ErrorMessage="Passwords must Match" ForeColor="Red">*</asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">Email</td>
                <td class="auto-style2">
                    <asp:TextBox ID="Email" class="Email" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="Email" ErrorMessage="Please enter an Email" ForeColor="Red">*</asp:RequiredFieldValidator>
                     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Email" ErrorMessage="Please Enter a valid Email" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style3">ConfirmEmail</td>
                <td class="auto-style2">
                    <asp:TextBox ID="ConfirmEmail" class="ConfirmEmail" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="ConfirmEmail" ErrorMessage="Please confirm Email" ForeColor="Red">*</asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="Email" ControlToValidate="ConfirmEmail" ErrorMessage="Emails must Match" ForeColor="Red">*</asp:CompareValidator>
                </td>
            </tr>
        </table>
   </div>
    <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="CreateAccount" Class="CreateAccount" runat="server" Text="Create Account" OnClick="SignUp_Click" />
    <br />
    <asp:Label  ID="Message" class="Message" runat="server" BackColor="Black" Font-Bold="True" Font-Size="30px" ForeColor="Red" Text="You have successfully created an Account" Height="30px" Visible="false"></asp:Label>
    <br />
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:portfoliobrownConnectionString2 %>" SelectCommand="SELECT [id], [FirstName], [LastName], [Password], [ConfirmPassword], [ConfirmEmail], [Email] FROM [SignUP]"></asp:SqlDataSource>
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
   <script type="text/javascript">
       $(".CreateAccount").click(function () {
           var first = document.getElementsByClassName("FirstName")
           var last = document.getElementsByClassName("LastName")
           var pass = document.getElementsByClassName("Password")
           var ConPass = document.getElementsByClassName("ConrirmPassword")
           var Email = document.getElementsByClassName("Email")
           var conEmail = document.getElementsByClassName("ConfirmEmail")
               if(first.length > 0 && last.length > 0 && pass.length > 0 && conPass.length > 0 &&
                   Email.length > 0 && conEnail.length > 0)
               {
                    alert("ehb");  
               }

           });

Donc, fondamentalement, je veux que la méthode d'alerte se déclenche après avoir cliqué sur le bouton pour créer le compte, mais uniquement lorsque la page est publiée à partir du serveur.

Merci, toute aide serait appréciée

0
user1491801 3 avril 2017 à 00:47

2 réponses

Meilleure réponse

Vous pouvez utiliser RegisterStartupScript pour cela. Créez une fonction javascript qui gère l'alerte.

<script type="text/javascript">
    function showAlert(msg) {
        alert(msg);
    }
</script>

Appelez ensuite cette fonction à partir du code derrière.

protected void Button1_Click(object sender, EventArgs e)
{
    string message = "Place message here";
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showAlert", "showAlert('" + message + "')", true);
}
0
VDWWD 3 avril 2017 à 07:01

Vous devez vérifier si l'entrée est vide ou non. Donnez à vos éléments d'entrée des balises d'identification comme ceci:

<input id="username" ...
<input id="password" ...

Ensuite, vérifiez-les logiquement dans JS.

<script>
    function button_onclick(){
         var name = document.getElementById('<%= username.ClientID %>').value;
         var passw = document.getElementById('<%= password.ClientID %>').value;
         if(name.length > 0 && passw.length > 0){
            alert("Fields are not empty!");
         }
    }
</script>
0
easeccy 2 avril 2017 à 23:08