locked
How to restrict user to go to back page by pressing default back arrow in windows -VB.NET-Datapager RRS feed

  • Question

  • User-1578974752 posted

    There are 3 pages in the Test. Each page having 5 questions, pager 1 ,2,3  is in List View.

    Now if I click the Back arrow mark (not in the form-Default one in the windows <-),it is still going to before page.

     If they click the back arrow of the near the link, they can go to previous page of List View.How can I avoid that .Appreciate the help

       <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="QuestDetailID" Visible="False" OnPagePropertiesChanging="ListView1_PagePropertiesChanging" OnDataBound="ListView1_DataBound">

          
                      
              
          
                <ItemTemplate>
                    
          <div>
              <td align="right">
                   <br />
                               <asp:Image ID="Image1" runat="server" ImageUrl="~/fail-small.png" /></td>
                            <td align="center">
               <td align="left"><%#DataBinder.Eval(Container.DataItem, "Question") %></td>
              <br />  
            
                               <asp:Image runat="server" id="image" ImageUrl='<%#Eval("Path")%>'/>          
              </div>
      <p>
      <tr style="background-color:White;">

     
    <tr><td align="left" colspan="3"> <asp:HiddenField ID="QuestionDetailID" runat="server" Value='<%#Eval("QuestDetailID") %>' />   </td></tr>

    <td align="left">     <asp:Label ID="Option1" runat="server" Text=' <%# Eval("Option1") %>'></asp:Label><asp:RadioButton ID="rad1" GroupName="Group1" runat="server" /></td>
     
    <td align="left"> <asp:Label ID="Option2" runat="server" Text=' <%# Eval("Option2") %>'></asp:Label>   <asp:RadioButton ID="rad2" GroupName="Group1" runat="server"  /></td>

    <td align="left"> <asp:Label ID="Option3" runat="server" Text=' <%# Eval("Option3") %>'></asp:Label>  <asp:RadioButton ID="rad3" GroupName="Group1" runat="server" /></td>
     
    <td align="left"> <asp:Label ID="Label1" runat="server" Text=' <%# Eval("Option4") %>'></asp:Label>  <asp:RadioButton ID="rad4"  GroupName="Group1" runat="server" /></td>
     

       
      </tr>

    </ItemTemplate>

          
              

        </asp:ListView>
     
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tionString %>" SelectCommand="SELECT TOP 15 [Question],[Name],[Path],[CorrectAnswer],[Option1], [Option2], [Option3], [Option4], [Option5], [QuestDetailID], [QuestionMasterID] FROM [QuestionDetail] WHERE ([QuestionMasterID] = @QuestionMasterID)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="qusetid" Name="QuestionMasterID" PropertyName="Text" Type="Int64" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <br />
                <br />
                <asp:Datapager ID="dtpgr" runat="server" pagedcontrolid="ListView1" Pagesize="5">

     

         
               <Fields>
                    <asp:NumericPagerField ButtonCount="5"

                NumericButtonCssClass="numeric_button"

                CurrentPageLabelCssClass="current_page"

                NextPreviousButtonCssClass="next_button" />
               </Fields>
            </asp:DataPager>

    Monday, November 12, 2018 6:15 AM

Answers

  • User839733648 posted

    Hi shsu,

    You  just could add the JS code mentioned above to your .aspx page, you do not have to write it to code-behind.

    Adding JS code in <head> tag.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <script type="text/javascript">
            function preventBack() { window.history.forward(); }
            setTimeout("preventBack()", 0);
            window.onunload = function () { null };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
    ......
        </form>
    </body>
    </html>
    

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2018 2:56 AM

All replies

  • User475983607 posted

    It is not possible to stop a user form clicking the back button.  You should always design your applications to work with the back button rather than trying find a hack the browser's behavior.

    Monday, November 12, 2018 1:27 PM
  • User-1578974752 posted

    Thanks  mgebhard.

    By clicking the below datapager,i am moving to the next page, but the link shown on the browser will be same. How can make in such a way that, if the user click page 2 and click the back arrow on the browser ,will show a msg and stay in that page , instead of going to previous page

    In the program ,if I click page 2 then page 1 link will be invisible.they can not go back.But from browser they can go back. .As it is test it is very important. Kindly advice

     <asp:Datapager ID="dtpgr" runat="server" pagedcontrolid="ListView1" Pagesize="5"> 

         
               <Fields>
                    <asp:NumericPagerField ButtonCount="5"

                NumericButtonCssClass="numeric_button"

                CurrentPageLabelCssClass="current_page"

                NextPreviousButtonCssClass="next_button" />
               </Fields>
            </asp:DataPager>

    Tuesday, November 13, 2018 1:32 AM
  • User839733648 posted

    Hi shsu,

    According to your description, I suggest that maybe you could add following JavaScript code to disable Browser Back Button.

    <script type="text/javascript" >
       function preventBack(){window.history.forward();}
        setTimeout("preventBack()", 0);
        window.onunload=function(){null};
    </script>

    the effect.

    For more about this, you could refer to the link: Disable Browser Back Button Functionality using JavaScript

    Best Regards,

    Jenifer

    Tuesday, November 13, 2018 6:30 AM
  • User-1578974752 posted

    I am not familiar with javascript.How to incorporate it with vb.net

    Thursday, November 15, 2018 9:13 AM
  • User839733648 posted

    Hi shsu,

    You  just could add the JS code mentioned above to your .aspx page, you do not have to write it to code-behind.

    Adding JS code in <head> tag.

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <script type="text/javascript">
            function preventBack() { window.history.forward(); }
            setTimeout("preventBack()", 0);
            window.onunload = function () { null };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
    ......
        </form>
    </body>
    </html>
    

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2018 2:56 AM
  • User-1578974752 posted

    Thanks a lot.

    Friday, November 16, 2018 7:02 AM