1

I have checked the code, but I can't find anything wrong.

<!-- language: lang-html -->

<div class="modal fade" id="loginModal" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div id="loginHeader" class="modal-header">
                <h4 class="modal-title">Oops, something not right.</h4>
            </div>
            <div id="loginContent" class="modal-body" runat="server">
            </div>
            <div id="loginButton" class="modal-footer">
                <button type="button" class="btn btn-warning" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
    <button type="button" style="display: none;" id="btnShowLoginModal" data-toggle="modal" data-target="#loginModal">
    </button>
</div>
<script src="../Scripts/javascripts/jquery.js"></script>
<script src="../Scripts/javascripts/bootstrap.min.js"></script>
<script type="text/javascript">
    function ShowLoginDialog() {
        $("#btnShowLoginModal").click();
    }
</script>

The function is called from code behind on OnClick Event. Still no clue which part is wrong.

protected void Button1_Click(object sender, EventArgs e)
{
    string result = ValidateLogin(Username.Value, Password.Value);

    if(result == MessageConstants.MessageSuccess)
    {
        Response.Redirect("~/Webpages/Character.aspx");
    }
    else if(result == MessageConstants.MessageFail)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "ShowLoginDialog()", true);
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "ShowLoginDialog()", true);

    }
}

Error from Element Inspector enter image description here

12
  • Where is it being called from? Please provide a minimal reproducible example Commented May 13, 2017 at 18:47
  • 1
    Where are you using the function? Commented May 13, 2017 at 18:47
  • Where you call that function ?? Commented May 13, 2017 at 18:49
  • You maybe invoking the function at the page's head? Commented May 13, 2017 at 19:00
  • Move the inline script (containing the function) before any other script tags, probably in the head tag. Commented May 13, 2017 at 19:02

3 Answers 3

2

RegisterStartupScript() emits the script block at the end of the Web Form (right before the </form> tag). source

But your modal code, and the ShowLoginDialog function are defined after that. Try moving that </form> tag to the end, after the modal stuff.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much! it actually works when i move the script into the <form> tag.
0

Put your code in the document.ready() function:

$( document ).ready(function() {
    function ShowLoginDialog() {
        $("#btnShowLoginModal").click();
    }
});

3 Comments

When you call that function, on page load or on any event ?
@SKJajoriya its actually OnClick event called that function
This will only make the issue worse because ShowLoginDialog will not be available on the global scope
0

Move

<script src="../Scripts/javascripts/jquery.js"></script>
<script src="../Scripts/javascripts/bootstrap.min.js"></script>
<script type="text/javascript">
    function ShowLoginDialog() {
        $("#btnShowLoginModal").click();
    }
</script>

into the head block. Then you need to change the ShowLoginDialog function so that it looks like this:

function ShowLoginDialog() {
    $(function() {
        $("#btnShowLoginModal").click();
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.