Search code examples
c#jsonbootstrap-modalcode-behindmodalpopup

trigger the modal with code behind C#


I have a Login form in asp.net, if the user isn't register into BD, I need that Bootstrap Modal is show (trigger), Its possible call json function from code behind, or send response redirect with the instruction to show modal?


Solution

  • Create a hidden field (asp:HiddenField with runat="server" and ID="myHiddenField" if you're using Web Forms) and in the code behind set the value of that hidden field (e.g.: myHiddenField.Value = "login-failed") if the login fails.

    Then, with some JavaScript/jQuery on the page, check the value of that hidden field, and if its value changed, open the modal. Something like this:

    $(function() {
        var hdnField = $("[id*=myHiddenField]");
        if (hdnField.val() === "login-failed") {
            $("#myModal").modal();
        }
    });