locked
Issue Client.registerstarrupscript, "clear" bootstrap modal RRS feed

  • Question

  • User-952476251 posted

    Hi everyone I have a problem with client. RegisterStartupScript: I created this event to make sure that every time there is an error a bootstrap modal appears.

    My problem is that when I go to another page and go back to the previous page, the modal is visible, I would like this behavior not to occur and I tried to use js but it fails and I would prefer to do it code behind side.

    Thursday, April 29, 2021 6:20 PM

All replies

  • User475983607 posted

    It is a lot easier to provide assistance if you share code the community can run that reproduces this behavior. 

    I can only guess that initial state of the page is an open modal.  The back button shows the page as it was initially rendered and is working as expected.  What troubleshooting steps have you performed?  Is the page loaded from cache?  Is the browser doing a GET?  

    Thursday, April 29, 2021 8:03 PM
  • User-952476251 posted
    <body>
        <form id="form1" runat="server">
            <div>
    
            <div id="myModal" class="modal fade" role="dialog">
                <div class="modal-dialog">
    
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Modal Header</h4>
                        </div>
                        <div class="modal-body">
                            <p>Some text in the modal.</p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
    
                </div>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <asp:ScriptManager runat="server"></asp:ScriptManager>
                <asp:Button ID="Button2" runat="server" Text="Continua" OnClick="Button2_Click" />
            </div>
        </form>
    </body>
    </html>

    CODE-BEHIND

        Protected Sub Button1_Click(sender As Object, e As EventArgs)
    
            Dim sb As StringBuilder = New StringBuilder()
            sb.Append("<script language='javascript'>")
            sb.Append("$('#myModal').modal('show');")
            sb.Append("</script>")
    
            ClientScript.RegisterStartupScript(Me.GetType(), "JSScript", sb.ToString())
    
        End Sub
    
        Protected Sub Button2_Click(sender As Object, e As EventArgs)
            Response.Redirect("Home.aspx")
        End Sub

    The second page has one button that return first page , i used Response.Redirect("Login.aspx")

    Thursday, April 29, 2021 8:45 PM
  • User475983607 posted

    The code is working as expected.  The problem is you hard coded the modal to show.  You would have the same problem if the error message is written to the page without a modal.

    Try the Post/Redirect/Get pattern.

    https://en.wikipedia.org/wiki/Post/Redirect/Get

    https://stackoverflow.com/questions/10827242/understanding-the-post-redirect-get-pattern

      

    Thursday, April 29, 2021 9:17 PM
  • User-952476251 posted

    Thanks for the resources, but I was wondering something else, suppose a user fills in some textboxes if he reaches a certain figure a modal appears showing an error message, he fills in correctly and sends the data which makes him go to the next page But once it is on the new page it goes back and the error modal appears when I loaded the page. There are no solutions that allow me to avoid this behavior. Unfortunately my code is not complete because I don't have the correct one and I was trying to simulate it.

    Thursday, April 29, 2021 9:25 PM
  • User475983607 posted

    Thanks for the resources, but I was wondering something else, suppose a user fills in some textboxes if he reaches a certain figure a modal appears showing an error message, he fills in correctly and sends the data which makes him go to the next page But once it is on the new page it goes back and the error modal appears when I loaded the page. There are no solutions that allow me to avoid this behavior. Unfortunately my code is not complete because I don't have the correct one and I was trying to simulate it.

    Um yeah, the Post/Redirect/Get pattern solves this problem.  Did you read the links???

    Thursday, April 29, 2021 9:27 PM
  • User-952476251 posted

    I will read them carefully, but I thought that It was possible clear o block this event.

    Thursday, April 29, 2021 9:32 PM
  • User475983607 posted

    I will read them carefully, but I thought that It was possible clear o block this event.

    The Post/Redirect/Get pattern solves this common programming problem.  

    Have you verified the event is firing when the back button is clicked?  Most likely the page is loaded from cache and the even never fires.   The network view in the browser's dev tools (F12) shows if the page is loaded from cache. 

    You could try disabling cache and forcing the page to reload when the back button is clicked.

    https://forums.asp.net/t/1304752.aspx?how+to+refresh+page+when+hitting+back+button

    Thursday, April 29, 2021 9:43 PM
  • User-952476251 posted

    Ok I will try it and if I stuck again, I will ask.

    Thanks!! 

    Thursday, April 29, 2021 9:51 PM